updating the last entry

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

updating the last entry

Post by nhan »

would this be correct?

Code: Select all

$query = &quote;UPDATE timein SET timeout='$Today',time2='$time' WHERE userName = '$name' and timeout = '' and timeId >= (LAST_INSERT_ID())&quote; or die(&quote;Couldn't query the user-database.&quote;);
i have to update the last entry record of the table, it seems that the "timeId >= (LAST_INSERT_ID())" is wrong can anyone help me on this one....

thanks! :D
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

how about just

Code: Select all

$query = "UPDATE timein SET timeout='$Today',time2='$time' WHERE userName='$name' AND timeout='' AND timeId=LAST_INSERT_ID()" or die(mysql_error());
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

thanks for your reply this doesnt work...

i am trying this one :

Code: Select all

UPDATE timein SET timeout='$Today',time2='$time' WHERE userName = '$name' and timeout = '' and timeId = 'MAX(ID)';
this also doesnt work...

i also tried this one :

Code: Select all

$query1 = &quote;SELECT MAX(ID) from timein where userName = '$user'&quote;;
$idmax = mysql_query($query1);
print $idmax;
$query = &quote;UPDATE MAX(timeId) timein SET timeout='$Today',time2='$time' WHERE userName = '$name' and timeout = '' and timeId='$idmax'&quote;  or die(&quote;Couldn't query the user-database.&quote;);
$result = mysql_query($query);
this also doesnt work, any work around on this one... thanks so much!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Just read: http://dev.mysql.com/doc/mysql/en/update.html

Query would look like (untested)

Code: Select all

UPDATE 
  timein
SET 
  timeout='$today',
  time2='$time'
WHERE 
  userName = '$name' and 
  timeout = ''
ORDER BY
  timeId DESC
LIMIT 1
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

hi tim,

the article does not explain the update with order by, i tried the code but it gives me an error :

ERROR 1064: You have an error in your SQL syntax near 'ORDER BY timeId DESC LIMIT 1' at line 1

thanks!
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

hi all,

is there any other way i can revise this code to get the maximum primary key (timeId)in the table...

Code: Select all

$query = &quote;UPDATE timein SET timeout='$Today',time2='$time' WHERE userName = '$name' and timeout = '' and timeId=SELECT MAX(timeId)&quote;;
or this code

Code: Select all

UPDATE   timein SET   timeout='$today',  time2='$time'WHERE   userName = '$name' and   timeout = ''ORDER BY  timeId DESC LIMIT 1
thanks!
Post Reply