bulrush Posted July 28, 2010 Share Posted July 28, 2010 - I am inserting a new record into a table called "parts". The key field is "partid", which is autoincremented. - After I do the insert, how do I get the value for partid for the new record? I cannot select the record because there are no unique combinations of fields to select on. Here is my code to insert the new record into table "parts". //Insert new part. $query="INSERT INTO parts (modelnum, prodcat, ". "prodname, prodsubname, createuser, createdate) " . "VALUES ( ". "'".$modelvar."', " . "'".$prodcatvar."', ". "'".$prodnamevar."', ". "'".$prodsubnamevar."', ". "'".$_SESSION['username']."', ". "NOW() ". ");"; $result=mysqli_query($dbc,$query); if (!$result) { $msg=mysql_error().'<br/>Could not run SELECT. '; //There was an error. crError($_SERVER['PHP_SELF'].' line '.__LINE__,$msg,true); } else { $s='Inserted new product cat='.$prodcatvar.', model='.$modelvar; crInfomsg($s); } } Thank you. Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/ Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 mysql_insert_id Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092234 Share on other sites More sharing options...
radar Posted July 28, 2010 Share Posted July 28, 2010 $id = mysql_insert_id(); but alex beat me to it Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092235 Share on other sites More sharing options...
bulrush Posted July 28, 2010 Author Share Posted July 28, 2010 So that returns the key field value of the previous INSERT statement in SQL? The autoinc field is the only key field. Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092238 Share on other sites More sharing options...
radar Posted July 28, 2010 Share Posted July 28, 2010 yes, if your current id is 725 and you run an insert, that id owuld be 726 mysql_insert_id() will retrieve 726 Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092241 Share on other sites More sharing options...
Mchl Posted July 28, 2010 Share Posted July 28, 2010 Just be careful on multi row inserts. It will return FIRST id that was generated. Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092242 Share on other sites More sharing options...
bulrush Posted July 28, 2010 Author Share Posted July 28, 2010 It's not working. I'm using: $dbc = mysqli_connect($host, $user, $password, $database); And: $partidvar=mysqli_insert_id($dbc); //Get key field of last INSERT statement. if (strlen($partidvar)==0) { $msg=mysql_error().'Could not get last partid.<br/>'.$query; //There was an error. crError($_SERVER['PHP_SELF'].' line '.__LINE__,$msg,true); } Should I use: mysqli_insert_id($dbc)? Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092256 Share on other sites More sharing options...
radar Posted July 28, 2010 Share Posted July 28, 2010 how i do mine: $sql = "my query"; $data = mysql_query($sql); $id = mysql_insert_id(); Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092258 Share on other sites More sharing options...
bulrush Posted July 28, 2010 Author Share Posted July 28, 2010 Thanks! Got it working. I did have to use the mysqli version. Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092259 Share on other sites More sharing options...
Mchl Posted July 28, 2010 Share Posted July 28, 2010 Just make sure you use mysqli_* functions everywhere. mysql_error() won't work with mysqli connection. Link to comment https://forums.phpfreaks.com/topic/209137-how-to-get-autoinc-field-value-after-sql-insert/#findComment-1092319 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.