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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.