update query syntax

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
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

update query syntax

Post by nhan »

hope anyone can help me on the syntax...

i will update a single row where the conditions are :
field 1 and field 2 have values and field 3 is empty i tried this code:

$query = "UPDATE tbl SET field3='$Today' WHERE userName = '$user' and data3 ='NULL'";

userName is field 2 and data3 should have the value of $today which is initially empty

i tried the one above but it doesnt insert the data... thanks so much!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$query = "UPDATE `tbl` SET `field3` = '$Today' WHERE `userName` = '$user' AND `data3` IS NULL";
The proper syntax is IS NOT not = NULL
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

thanks so much for the reply but i have already tried that one but it does not update the row, should the varilbles field3 username and data3 be enclosed with single quotes(')????

thanks again!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no.. they should be surrounded by backticks:

` vs. '
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

update query syntax

Post by nhan »

i have already figured out what is wrong thanks so much every body, there is a big difference in using "NULL" and with the plain old single quotes('')....
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

that NULL <> 'NULL' is obvious ;)

the unobvious one is this: NULL <> NULL is also true. (For an sql dbms there is a logical explanation.. but for a relational database it's imho bollocks.)
Post Reply