PHP/MYSQL: Update Query where value is missing from WHERE clause

953

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.

396

Answer

Solution:

Both queries will update all records whereuserid 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.

765

Answer

Solution:

If the variable is empty, or without a value, or "", and on your table there is no record whereuserid 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.

People are also looking for solutions to the problem: Php code that is like a public variable in java?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.