jponte Posted February 20, 2010 Share Posted February 20, 2010 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 Link to comment https://forums.phpfreaks.com/topic/192751-mysql_insert_id-not-picking-the-last-record-number/ Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 You didn't run the insert query first... Link to comment https://forums.phpfreaks.com/topic/192751-mysql_insert_id-not-picking-the-last-record-number/#findComment-1015366 Share on other sites More sharing options...
jponte Posted February 20, 2010 Author Share Posted February 20, 2010 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 Link to comment https://forums.phpfreaks.com/topic/192751-mysql_insert_id-not-picking-the-last-record-number/#findComment-1015370 Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 No you didn't. You just assigned a query string to a variable. You run your queries using mysql_db_query() function later in your code. Link to comment https://forums.phpfreaks.com/topic/192751-mysql_insert_id-not-picking-the-last-record-number/#findComment-1015376 Share on other sites More sharing options...
greatstar00 Posted February 20, 2010 Share Posted February 20, 2010 if u want the auto_increment id, without insert into the table SHOW TABLE STATUS LIKE 'tablename' the next auto id will be somewhere Link to comment https://forums.phpfreaks.com/topic/192751-mysql_insert_id-not-picking-the-last-record-number/#findComment-1015401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.