Foreign Key problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
shoapin
Forum Newbie
Posts: 4
Joined: Tue Dec 27, 2011 2:56 pm

Foreign Key problem

Post by shoapin »

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));
}
?>
whiterainbow
Forum Newbie
Posts: 11
Joined: Fri Mar 18, 2011 9:13 am

Re: Foreign Key problem

Post by whiterainbow »

I may be just being stupid here, but your constraint references customer_id and the actual field name seems to be custId. Do you have a synonym to relate between the two?
Post Reply