Jump to content

mysql_insert_id() not picking the last record number


jponte

Recommended Posts

Hi Freaks,

I cannot get to mysql_Insert_ ID () to pick up the last enter Auto_Increment number generated.

 

//Set variables for database access
$Host = "localhost";
$User = "root";
$PasswordDB = "escrma";
$DBName = "rma_portal";

//Connect to Database
$Link = mysql_connect ($Host, $User, $PasswordDB);
mysql_select_db("rma_portal") or die(mysql_error());


// Collects data from rma_contact table 
$contactqry = mysql_query("SELECT * FROM rma_contact
				 WHERE Cont_Attention = '$ShipAttention' AND Cont_Email = '$ShipEmail'") 

or die('Could not run query: ' . mysql_error());

// puts the "rma_contact" info into the $contactinfo array if found
$contactinfo = mysql_fetch_assoc($contactqry);  

$ContactIDVal = $contactinfo['Contact_ID'];

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')";

//Get the generated ID for Contact_ID	
$Gen_Contact_ID = mysql_insert_id(); //DOES NOT PICK THE VALUE HERE

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

echo $Gen_Contact_ID;

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

$Gen_Contact_ID = $contactinfo ['Contact_ID'];

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

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

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");

if (mysql_db_query ($DBName, $Query2, $Link)) {
print ("The RMA form 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)) {
print ("The RMA form 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(); 
}

mysql_close ($Link);

 

I tried changing thing around but it does not grab the last number generate for a CustomerID

 

Thanks for your help

Hi,

I did:

 

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')";

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

 

Thanks for the reply

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.