Jump to content

best way to get the id of the post you just posted?


shortysbest

Recommended Posts

I have a status system, built with ajax, and I need to get the unique id that the database assigns to the post and return it in the results that are appended to the page via javascript, however I'm not sure how the bessst way would be to do this? I have been doing this:

 

 ///variables that are sent to the database
$session = $_COOKIE['id'];
$uid = $_POST['id'];
$post = $_POST['post'];
$date = mktime();

//way I have got the unique id of this post
$query = mysql_fetch_assoc(mysql_query("SELECT * FROM posts WHERE from_id='$session' AND to_id='$uid' AND post='$post' AND date='$date'"));
$id = $query['id'];

 

Although that does work, It feels like it might not always be accurate.

after a record insert with mysql_query, you can get the id of that record with mysql_insert_id(); simple example:

 

$sql = "INSERT INTO some_table SET post_text = '$post_text'";
mysql_query ($sql) or die(mysql_error());
$new_record_id = mysql_insert_id();

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.