After some simple code
I'm using ODBC
I have several tables, more than what I'll list here
Person
Address
State
Country
State is a field in Address which is a FK to Abbreviation, the PK in the table State
The issue I have is, you enter data in a form, it searches the tables to see if State or Country exists, if it doesn't it updates the table
When I perform the operation of adding a new person which gets the person's details, address details, etc, it adds to the Person table fine, it never updates the address, state or country table, it gives me the error
You cannot add or change a record because a related record is required in table 'State'.
And points to a line in the Address function, the one where it executes the SQL statement
The trouble I have with that is, the Address function isn't called until after the State and Country functions
Enter_New_State($Abbrev, $State);
Enter_New_Country($Country);
Enter_New_Address($AddressLn1 ,$AddressLn2 ,$Suburb, $State, $Postcode, $Country, $PrefPhone, $AltPhone);
I tried to put some data manually in the table that I knew the address would use, as I'm filling the form out with some dummy data
I filled out the form again, gave me the same error
Yet if I enter all the information into the address, state and country table manually it works. Person all ready have being uploaded by the form
Here's the code to my state function
function Enter_New_State($Abbrev, $State) {
/*
First, we create a connection to our ODBC source. This is done by creating
a connection. Once this is done, we are returned an ODBC connection number.
We use this number to use the ODBC functions within PHP.
*/
$cnx = odbc_connect( 'project_draft' , 'root', '' );
if (!$cnx) {
Error_handler( "Error in odbc_connect" , $cnx );
}
$cur0= odbc_exec( $cnx, "select * from State where State.abbreviation = '$_POST[abbreviation]'" );
$nbrows=0;
while( odbc_fetch_row( $cur0 ) ) {
$nbrows++;
$Abbreviation = odbc_result( $cur0, 1 ); } //Get's abbreviation field
if (!isset($cur0)){
// statement to execute
$SQL_Exec_String = "Insert Into State ( Abbreviation, Name) Values ( '$_POST[Abbreviation]', '$_POST[Name]')";
// in this connection execute this string
$cur= odbc_exec( $cnx, $SQL_Exec_String );
if (!$cur) {
Error_handler( "Error in odbc_exec( no cursor returned ) " , $cnx );
}}else{/*do nothing, is that possible maybe exit;*/}
odbc_close( $cnx);
}
Here's the code to my address function
function Enter_New_Address($AddressLn1 ,$AddressLn2 ,$Suburb, $State, $Postcode, $Country, $PrefPhone, $AltPhone)
{
/*First, we create a connection to our ODBC source. This is done by creating
a connection. Once this is done, we are returned an ODBC connection number.
We use this number to use the ODBC functions within PHP. */
$cnx = odbc_connect( 'project_draft' , 'root', '' );
if (!$cnx) {
Error_handler( "Error in odbc_connect" , $cnx );
}
// increments PID for UID
$cur0= odbc_exec( $cnx, "select * from Person" );
$nbrows=0;
while( odbc_fetch_row( $cur0 ) ) {
$nbrows++;
$PersonID= odbc_result( $cur0, 1 ); }
$cur=odbc_exec( $cnx, "select * from Address");
$rows=0;
while(odbc_fetch_row($cur)){
$rows++;
$AddressID=odbc_result ($cur, 2);}
$AddressID++;
// statement to execute
$SQL_Exec_String = "Insert Into Address ( PersonID, AddressID, AddressLn1, AddressLn2, Suburb, State, Postcode, Country, PrefPhone, AltPhone) Values ( $PersonID, $AddressID, '$_POST[AddressLn1]', '$_POST[AddressLn2]', '$_POST[Suburb]', '$_POST[Abbreviation]', '$_POST[Postcode]','$_POST[Country]', '$_POST[PrefPhone]', '$_POST[AltPhone]')";
// in this connection execute this string
$cur= odbc_exec( $cnx, $SQL_Exec_String ); //This is the line it points to
if (!$cur) {
Error_handler( "Error in odbc_exec( no cursor returned ) " , $cnx );
}
odbc_close( $cnx);
}
Now I'm not sure if I got PersonID right here as it is meant to get the PersonID out of the PersonID table and put it in the Address Table as the PK is made up of PersonID and AddressID but that's not the issue I'm having at the moment. Every function is similar in it's makeup - originally ripped from a tute.