Reputation: 33
I have a form with two stages, the first is to put the information but the second is the UPDATE to add the photo. So what I want to do is retrieve the last auto incremented ID to update the data, I used LAST_INSERT_ID ()
but I realize it is the update everywhere. Do you have a suggestion for me to help me? Thank you and sorry sorry for my english.
You can see here what I did:
$sql = 'UPDATE 'jj_news' SET
cover_picture="'.$large_image_name.$_SESSION['user_file_ext'].'",
min_picture="'.$thumb_image_name.$_SESSION['user_file_ext'].'", statut="1",
edited="'.date('Y-m-d h:i:s').'" WHERE id_news= LAST_INSERT_ID(id_news)';
mysql_query($sql) or die('Erreur SQL !'.$sql.'<br />'.mysql_error());
Upvotes: 3
Views: 180
Reputation: 760
Bloody hell, why don't you just store the damn id, into a hidden post field and pass it to the second page on form submit?
Upvotes: 1
Reputation: 32
You can try the following function.
function getLastId($tablename,$id_field){
$id=0;
$sql="select ".$id_field." from ".$tablename." order by ".$id_field." desc limit 1" ;
$res_obj=mysql_query($sql) or die(mysql_error);
if(mysql_num_rows($res_obj)>0){
$result=mysql_fetch_array($res_obj); //we have only on row and column.
$id=$result[$id_field]; //set the last greator Id value;
}
return $id;
}
$newid=getLastId("yourtable","id_field")+1;
//insert your data using $newid instead of autocomplete.
//use $newid during update if update is on new page make a session of $newid. like
$_SESSION['update_id']=$newid;
this function make manual increment of id column, instead of auto increment. I think it is ease to control your situation using this function.
Upvotes: -1
Reputation: 1852
mysql_query("INSERT INTO `xy` (`xy`) VALUES ('$_POST[xy]');");
$insertId = mysql_insert_id();
mysql_query("UPDATE xy SET xy='$xy' WHERE id='$insertId'");
only know from the mysql_* function...
use mysqli or PDO, but if this helps you i´m glad.....
Upvotes: -1
Reputation: 157828
You cannot get your id in the "second stage". You have to get it immediately after running insert.
So, get it in the first stage, store it in a session and then use in the second one.
Upvotes: 2