-1

I am trying to update a value into my table with the last ID, but I can not get mysql_insert_id() working. Can someone help me (I am a beginner with PHP). Thanks in advance!

This is a piece of my code, I know that mysql is outdated but I am doing this as a school project:

$tijd = date("H:i:s");
$userid = mysql_insert_id();        
$query = "UPDATE tijden SET tijduit = '$tijd' WHERE id = '$userid'";
$resultaat = mysql_query($query);    
unset($_SESSION['inchecken']);

1 Answers1

-1
$tijd = date("H:i:s");
$userid = mysql_insert_id();  

You cannot get a mysql_insert_id() without doing an INSERT query. If you are trying to retrieve the last record do the following query:

SELECT id FROM tijden ORDER BY id DESC LIMIT 1

From there get the last ID and use this variable to update the record below.

$query = "UPDATE tijden SET tijduit = '$tijd' WHERE id = '$userid'";
$resultaat = mysql_query($query);    

You do not necessarily need to separate the $query variable from the function. Simplify by doing:

$resultaat = mysql_query("UPDATE tijden SET tijduit='$tijd' WHERE id='$userid'");