TimUSA Posted January 6, 2008 Share Posted January 6, 2008 I though I followed this right in the manual, but apparently not ??? $raceresult = mysql_query(" SELECT mysql_insert_id() FROM race_table "); while ($row2 = mysql_fetch_row($raceresult)){ $raceID = $row2[0]; //enter information to the race_screenshots_table mysql_real_escape_string($_POST['image']); mysql_query(" INSERT INTO race_screenshots_table (imageURL, raceID) VALUES ('{$_POST['image']}', '{$raceID}')"); } Quote Link to comment https://forums.phpfreaks.com/topic/84695-mysql_insert_id/ Share on other sites More sharing options...
chronister Posted January 6, 2008 Share Posted January 6, 2008 mysql_insert_id() can only be used AFTER an insert query is ran and you set it to a var like so <?php $query="SELECT * FROM tablea WHERE this=$that"; $result=mysql_query($query); $lastID=mysql_insert_id(); /* you may need to pass the handle of the query var into this.. in my case here it would be mysql_insert_id($result);*/ echo 'The id of the last record inserted was '. $lastID; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84695-mysql_insert_id/#findComment-431646 Share on other sites More sharing options...
Barand Posted January 6, 2008 Share Posted January 6, 2008 As the value of the last insert id is "per connection" it has to made immediately after the insert was made. Once the script has finished the connection is closed and it is no longer available. This also ensures the it is the last id you inserted in your connection and not that of another user on another connection. If you just have a SELECT as above, it is null. Quote Link to comment https://forums.phpfreaks.com/topic/84695-mysql_insert_id/#findComment-431790 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.