Jump to content

Getting the id of the next primary key


snowdog

Recommended Posts

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());  


 

Link to comment
https://forums.phpfreaks.com/topic/53942-getting-the-id-of-the-next-primary-key/
Share on other sites

<?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

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()); 

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());

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.