Foreign Key

I need help with the foreign key, this is the php portion of my site to insert records, i am testing so i am only entering one. When i hit the creat account button it sends me to another page to insert additional info. I add info to the first page no problem but when i add to the 2nd page and submit i get the error: Cannot add or update a child row: a foreign key constraint fails (search_info.properties, CONSTRAINT properties_ibfk_1 FOREIGN KEY (customer_id) REFERENCES information (customer_id) ON DELETE CASCADE ON UPDATE CASCADE). Can someone help please?

1st Page NewAccount
DATABASE INFO - Table name Information
# Column Type Collation Attributes Null Default Extra Action
1 custId int(11) No None AUTO_INCREMENT Change Drop More
2 Username varchar(25) latin1_swedish_ci No None Change Drop More
INDEXES
Action Keyname Type Unique Packed Column Cardinality Collation Null Comment
Edit Drop PRIMARY BTREE Yes No custId 1 A

$editFormAction = $_SERVER[‘PHP_SELF’];
if (isset($_SERVER[‘QUERY_STRING’])) {
$editFormAction .= “?” . htmlentities($_SERVER[‘QUERY_STRING’]);
}

if ((isset($_POST[“MM_insert”])) && ($_POST[“MM_insert”] == “newAccount”)) {
$insertSQL = sprintf(“INSERT INTO information (Username) VALUES (%s)”,
GetSQLValueString($_POST[‘accUser’], “text”));

mysql_select_db($database_PropertyConnect, $PropertyConnect);
$Result1 = mysql_query($insertSQL, $PropertyConnect) or die(mysql_error());

$insertGoTo = “listingType.php”;
if (isset($_SERVER[‘QUERY_STRING’])) {
$insertGoTo .= (strpos($insertGoTo, ‘?’)) ? “&” : “?”;
$insertGoTo .= $_SERVER[‘QUERY_STRING’];
}
header(sprintf(“Location: %s”, $insertGoTo));
}

2nd Page Additional Info
DATABASE INFO - Table name properties
# Column Type Collation Attributes Null Default Extra Action
1 propId int(11) No None AUTO_INCREMENT Change Drop More
2 FirstName varchar(25) latin1_swedish_ci No None Change Drop More
3 custId int(11) No None Change Drop More

INDEXES

Action Keyname Type Unique Packed Column Cardinality Collation Null Comment
Edit Drop PRIMARY BTREE Yes No propId 0 A
Edit Drop custId BTREE No No custId 0 A

$editFormAction = $_SERVER[‘PHP_SELF’];
if (isset($_SERVER[‘QUERY_STRING’])) {
$editFormAction .= “?” . htmlentities($_SERVER[‘QUERY_STRING’]);
}

if ((isset($_POST[“MM_insert”])) && ($_POST[“MM_insert”] == “freeList”)) {
$insertSQL = sprintf(“INSERT INTO properties (FirstName) VALUES (%s)”,
GetSQLValueString($_POST[‘freeCountryList’], “text”));

mysql_select_db($database_PropertyConnect, $PropertyConnect);
$Result1 = mysql_query($insertSQL, $PropertyConnect) or die(mysql_error());

$insertGoTo = “freeRegistration.html”;
if (isset($_SERVER[‘QUERY_STRING’])) {
$insertGoTo .= (strpos($insertGoTo, ‘?’)) ? “&” : “?”;
$insertGoTo .= $_SERVER[‘QUERY_STRING’];
}
header(sprintf(“Location: %s”, $insertGoTo));
}
?>

i don’t do php, but this part is wrong –

INSERT INTO properties (FirstName) VALUES (%s)

you aren’t setting a value for the custId column

I think you have should have just one one table here, as it looks as if custId, username and FirstName are all attributes of the customer, and so belong in the same table.

Also the error message says customer_id, and NEITHER table has a field of that name…

Ok thank you for the speedy responses. The first table contains username password, names, addresses, phone number, etc. While the second table contain listings for properties whether it be rent, sale, house apartment, etc. I only used the one value to make my post smaller. What i want to do when a potential customer comes to the website they would insert their personal information, then moving on to there listings. That way whenever they log in and want to update the listing they have it would list all there listing under the customer id, thus the reason i have two tables and am using a foreign key. I am using dreamweaver and the insert behaviour and i dont know where i can add the custId because it is not enter by the user but generated when a user starts their process. So i’m totally lost

you need to use the LAST_INSERT_ID() function

i have no idea how to make dreamweaver do that (this is the databases/mysql forum)

[fphp]mysql_insert_id[/fphp] :slight_smile: