Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.