Update MySQL Entry

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
partiallynothing
Forum Commoner
Posts: 61
Joined: Fri Nov 21, 2003 5:02 pm
Location: connecticut, usa

Update MySQL Entry

Post by partiallynothing »

I want to update the datbase for the specified user with the new "last login" date.

Code: Select all

<?php
		$query = "UPDATE lastvisit SET $currentdate FROM users WHERE username = $username AND password = $password";
		$resut = mysql_query($query) or die(mysql_error());
?>
I get an error with the above code thats soemthing along the lines of "You have an error in your SQL syntax near '2003-11-28 FROM users WHERE username ='username' AND password = 'password'' at line 1"

Am I using incorect syntax. Thanks!
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

My code assumes that the table is called users, the field is called lastvisit, and the current date is $currentdate:

Code: Select all

<?php
      $query = "UPDATE users SET lastvisit = $currentdate WHERE username = $username AND password = $password";
      $resut = mysql_query($query) or die(mysql_error());
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Also, be sure to add ' around the values when inserting/updating etc., fields in the database to start with. Some more reading up on functions to be using with this might be for example: mysql_escape_string()

Code: Select all

$query = "UPDATE users SET lastvisit = '$currentdate' WHERE username = '$username' AND password = '$password'";
      $resut = mysql_query($query) or die(mysql_error());
Post Reply