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. Quote 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 Quote 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 Quote 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. Quote 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 Quote 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. Quote 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)? Quote 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(); Quote 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. Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.