Jump to content

mysql_insert_id() not passing the number


jponte

Recommended Posts

Hi PHP Freaks,

I'am able to pick the last record created but I cannot pass it to the second query. What I'm I doing wrong?? Going crazy

 

if(!isset($ContactIDVal)){

$Query2 = "INSERT into rma_contact (Cont_Attention, Cont_Phone, Cont_Email, Cont_Address, Cont_Add_Address, Cont_City, Cont_Prov, Cont_Postal, Cont_Add_Info) values ('$ShipAttention', '$ShipPhone', '$ShipEmail', '$ShipStreet', '$ShipAddinfo', '$ShipCity', '$ShipProv', '$ShipPCode', '$ShipAddNotes')";


$Query = "INSERT into rma_devices values ('$RMANumber', '$CustomerID', '$RMADate', '$DevicePhone', '$DeviceType', '$DeviceModel', '$DeviceIMEI', '$DeviceColor', '$DescDefect', '$ResetDevice', '$SoftwareReload', '$WipeDevice', '$TroubOther', '$TroubNotes', '$Gen_Contact_ID')";  //does not display the Gen_Contact_ID

	if (mysql_db_query ($DBName, $Query2, $Link)) {

	//Get the generated ID for Contact_ID	
	$Gen_Contact_ID = mysql_insert_id();

	//Print the Insert id
	printf("Last inserted record has id %d\n", mysql_insert_id());

	echo $Gen_Contact_ID; //PRINTS OK

	print ("<br>The Query 2 has been saved to the ESC database<br>\n");

	} else {print ("There was a problem saving the RMA form to the database<br>Please contact ESC at 1-877-939-3282\n");
			echo "<BR><BR>Error message = ".mysql_error(); 
	}

	if (mysql_db_query ($DBName, $Query, $Link)) {

	echo $Gen_Contact_ID; //PRINTS OK

	print ("<br> The Query 1 has been saved to the ESC database<br>\n");
	} else {print ("There was a problem saving the RMA form to the database<br>Please contact ESC at 1-877-939-3282\n");
			echo "<BR><BR>Error message = ".mysql_error(); 
	}
}
else{

$Gen_Contact_ID = $contactinfo ['Contact_ID'];

print ("<BR>Break: $Gen_Contact_Info<BR>\n");

$Query3 = "INSERT into rma_devices values ('$RMANumber', '$CustomerID', '$RMADate', '$DevicePhone', '$DeviceType', '$DeviceModel', '$DeviceIMEI', '$DeviceColor', '$DescDefect', '$ResetDevice', '$SoftwareReload', '$WipeDevice', '$TroubOther', '$TroubNotes', '$Gen_Contact_ID')";

	if (mysql_db_query ($DBName, $Query3, $Link)) {

	echo $Gen_Contact_ID;

	print ("<br> The Query 1 has been saved to the ESC database<br>\n");
	} else {print ("There was a problem saving the RMA form to the database<br>Please contact ESC at 1-877-939-3282\n");
			echo "<BR><BR>Error message = ".mysql_error(); 
	}

}

if (!$link = mysql_connect('localhost', 'root', 'escrma')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('rma_portal', $link)) {
    echo 'Could not select database';
    exit;
}

print ("<br>The Query2 is:<BR>$Query2<P>\n");
print ("<br>The Query1 is:<BR>$Query<P>\n");
print ("<br>The Query3 is:<BR>$Query3<P>\n");

mysql_close ($Link);

Link to comment
https://forums.phpfreaks.com/topic/192893-mysql_insert_id-not-passing-the-number/
Share on other sites

You do understand that code is executed from top to bottom and that variables don't have values in them until the code that assigns a value has been executed.

 

You are trying to use the variable $Gen_Contact_ID in your code that is building the string in $Query before $Gen_Contact_ID has been assigned a value. Are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your php.ini so that php would report and display all the errors it detects? You would be getting a undefined error concerning $Gen_Contact_ID when you try to use it before it has been assigned a value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.