PHP/MYSQL: Update Query where value is missing from WHERE clause
I think I know the answer to this but want to get confirmation.
If you have a simple update query with a where clause and the value of the variable in the where clause is missing, does that mean every record gets updated?
//let's say $name is empty
UPDATE users SET name= 'Jason' WHERE userid = '$name'
is that the same as
UPDATE users SET name= 'Jason' WHERE userid = ''
Is the behavior that every record would get updated?
And, if so, is there anything you can put in the SQL to prevent this potential catastrophe?
Thanks for guidance.
Answer
Solution:
Both queries will update all records where
userid
column is empty. But it doesn't means that rows withuserid
value which is equal toNULL
will be affected. If you want to useWHERE
againstNULL
column, read how to working with NULL values.Answer
Solution:
If the variable is empty, or without a value, or "", and on your table there is no record where
userid
is empty or null, then no record will be updated as the where-condition would favor no record, however if there is any record with emptyuserid
, then the record will be updated with the values you provided to the respective columns.