PDA

View Full Version : Adding records to database using foreign key


shoapin
03-04-2012, 06:11 PM
I am trying to insert records into two tables and link them with a foreighn key, and i honestly am totally lost, i tried asking around but no one seems able to provide an answer or examples.

Basically i am and user info eg. first name, last name etc on one page, this goes to a database called information and then i send them to another page which insert info into a properties database and i try to link with a foreign key but i am getting the error:

Cannot add or update a child row: a foreign key constraint fails (`search_info`.`properties`, CONSTRAINT `properties_ibfk_1` FOREIGN KEY (`custId`) REFERENCES `information` (`custId`) ON DELETE CASCADE ON UPDATE CASCADE)

Here is my php

This the user info page
<?php
$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));
}
?>
html..... with a table which u insert the data into

This the properties info page
</php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
mysql_select_db($database_PropertyConnect, $PropertyConnect);
$query_rsUserInfo = "SELECT information.custId, information.Username FROM information";
$rsUserInfo = mysql_query($query_rsUserInfo, $PropertyConnect) or die(mysql_error());
$row_rsUserInfo = mysql_fetch_assoc($rsUserInfo);
$totalRows_rsUserInfo = mysql_num_rows($rsUserInfo);
echo $query_rsUserInfo;
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "freeList")) {
$insertSQL = sprintf("INSERT INTO properties (custId) VALUES (%s), 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));
}

?>
htm table to insert info.....

Can someone please help me????