snowdog Posted June 2, 2007 Share Posted June 2, 2007 I am inserting a new row when the user adds a new message to database. For logic purposes later down the road I need the $associated to equal the new row id which is a primary key on auto increment. I can't seem to find what I am looking for anywhere. Can this be done. ie: the last message is id 8 so this new insertion would be id 9. I need associated to be the value of 9 on the insert also. Thanks, Snowdog $query = "INSERT INTO email_news (level,author,date,time,subject,message,associated,display) VALUES ('$level','$author','$date','$time','$subject','$message','$associated','1')"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/53942-getting-the-id-of-the-next-primary-key/ Share on other sites More sharing options...
per1os Posted June 2, 2007 Share Posted June 2, 2007 <?php $query = "INSERT INTO email_news (level,author,date,time,subject,message,display) VALUES ('$level','$author','$date','$time','$subject','$message','1')"; mysql_query($query) or die('Query failed: ' . mysql_error()); $lastid = mysql_insert_id(); $nextid = ($lastid + 1); $query = "UPDATE email_news SET associated = " . $nextid . " WHERE emailid = " . $lastid . ";"; mysq_query($query) or die('Query failed: ' . mysql_error()); ?> www.php.net/mysql_insert_id Quote Link to comment https://forums.phpfreaks.com/topic/53942-getting-the-id-of-the-next-primary-key/#findComment-266693 Share on other sites More sharing options...
snowdog Posted June 2, 2007 Author Share Posted June 2, 2007 Hmmm it didint work. I am wanting to insert it as I am also at the exact same time creating the new entry. See new code. $author = $auth_pc_member->user_id; $date = date("Y-m-d"); $time = date("H:i:s"); $subject = $_POST['subject']; $message = $_POST['editor1_content']; $message = html_entity_decode($message,ENT_COMPAT); $message = str_replace('\"', '"',$message); // get level of author $query = "select * from userdata WHERE id = '$author'"; $result_userdata = mysql_query($query) or die('Query failed: ' . mysql_error()); $show_userdata = mysql_fetch_object($result_userdata); $level = $show_userdata->level; $lastid = mysql_insert_id(); $query = "INSERT INTO email_news (level,author,date,time,subject,message,associated,display) VALUES ('$level','$author','$date','$time','$subject','$message','$last_id','1')"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/53942-getting-the-id-of-the-next-primary-key/#findComment-266694 Share on other sites More sharing options...
marcus Posted June 2, 2007 Share Posted June 2, 2007 Just do: $query = "INSERT INTO email_news (level,author,date,time,subject,message,associated,display) VALUES ('$level','$author','$date','$time','$subject','$message','$last_id','1')"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); $lastid = mysql_insert_id(); $sql = "UPDATE `email_news` SET `associated` =$lastid WHERE `id` = $lastid"; $res = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/53942-getting-the-id-of-the-next-primary-key/#findComment-266698 Share on other sites More sharing options...
snowdog Posted June 2, 2007 Author Share Posted June 2, 2007 so simple I didnt even see it. Thanks. I also thought there was a way to do it in one shot. If there is that would be great to know just to be efficient as possible with my code. Snowdog Quote Link to comment https://forums.phpfreaks.com/topic/53942-getting-the-id-of-the-next-primary-key/#findComment-266701 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.