简体   繁体   中英

Set all rows in SQL where id='$id'

I have made an online Police Dispatching program and I am trying to add a new feature. I want to add a button to set all Police Officer's status to 'On Duty'. This is my current action for the form/submit button:

<?php

mysqli_query($con,"UPDATE users SET status='0' WHERE code='$code'");

mysqli_close($con);
printf("<script>location.href='../units.php'</script>");
?>

The 'On Duty' is the same as 0, busy is 1, and unavailable is 2. The code is basically the Police Department's unique code that all Officers have. Right now, this is affecting any rows! Please help!

If you want ALL to be set to Zero, as you say, just Drop your WHERE statement.

"UPDATE users SET status='0'";

You should drop the Single Quotes as well, but it's not hurting anything.

"UPDATE users SET status=0";

Database has this security that don't allow users to update all data when you are using a UPDATE statement without WHERE clause, you can do a hacky query like this

UPDATE users SET status = 0 WHERE code != 'any value'; // if code is string

UPDATE users SET status = 0 WHERE code <> 0; // if integer, just makes sure 0 is not a code in your DB

all rows that didn't match in the value will be updated, in this case, all rows will be updated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM