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
Related
I want to only run the update query if row exists (and was inserted). I tried several different things but this could be a problem with how I am looping this. The insert which works ok and creates the record and the update should take the existing value and add it each time (10 exists + 15 added, 25 exists + 15 added, 40 exists... I tried this in the loop but it ran for every item in a list and was a huge number each time. Also the page is run each time when a link is clicked so user exits and comes back
while($store = $SQL->fetch_array($res_sh))
{
$pm_row = $SQL->query("SELECT * FROM `wishlist` WHERE shopping_id='".$store['id']."'");
$myprice = $store['shprice'];
$sql1 = "insert into posted (uid,price) Select '$uid','$myprice'
FROM posted WHERE NOT EXISTS (select * from `posted` WHERE `uid` = '$namearray[id]') LIMIT 1";
$query = mysqli_query($connection,$sql1);
}
$sql2 = "UPDATE posted SET `price` = price + '$myprice', WHERE shopping_id='".$_GET['id']."'";
$query = mysqli_query($connection,$sql2);
Utilizing mysqli_affected_rows on the insert query, verifying that it managed to insert, you can create a conditional for the update query.
However, if you're running an update immediately after an insert, one is led to believe it could be accomplished in the same go. In this case, with no context, you could just multiply $myprice by 2 before inserting - you may look into if you can avoid doing this.
Additionally, but somewhat more complex, you could utilize SQL Transactions for this, and make sure you are exactly referencing the row you would want to update. If the insert failed, your update would not happen.
Granted, if you referenced the inserted row perfectly for your update then the update will not happen anyway. For example, having a primary, auto-increment key on these rows, use mysqli_insert_id to get the last inserted ID, and updating the row with that ID. But then this methodology can break in a high volume system, or just a random race event, which leads us right back to single queries or transaction utilization.
I currently have a query where I delete a record on call, however after other consideration I would rather just update the record on a is_deleted basis so that I can always have a record of whats been in the system, and or undelete that record at a later time.
My current query:
$delete=mysql_query("DELETE FROM pin_status_types WHERE pinstatus_id='$delete'");
Instead of deleteing the record, i would rather change a value in a column from 0 to 1.
0 = false (not deleted)
1 = true (is deleted)
Correct me if I wrong, but wouldnt I do (Note; I added table. and column. to for note purposes.) something like below to achieve what I am after?
$delete=mysql_query("UPDATE table.pin_status_types SET column.is_deleted = 1 WHERE pinstatus_id='$delete'");
Update your query to:
mysql_query("UPDATE pin_status_types SET is_deleted = 1, date_deleted = NOW() WHERE pinstatus_id = '{$delete}'"
Also, as David suggested in comments, you might want to add timestamp for when a record was deleted for audit purposed.
Update: changed query to cover the issue raised in your comment. Make the date_deleted column default to 0 instead on CURRENT_TIMESTAMP. See this question for more details on that.
Please learn about and use PDO going forward
[UPDATE: found solution. see my own answer, below]
i'm trying to learn php and i can't figure how to do something that seems extremely basic, i.e. update a record based on fetching criteria. here's the part of my code that successfully fetches the record:
mysql_select_db('top_choice_system');
$query = "SELECT item_name FROM main ORDER BY cw DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "-- item name --" . $row['item_name'];
how do i update the specifically fetched item?
ftr below is the code i successfully use to update a record. however, in the code below, i specify the record myself in the WHERE portion:
$sql = 'UPDATE main
SET column5=28
WHERE ITEM=15';
my point is i can't figure how to make the WHERE match the fetched record. (or, better yet, how to fetch AND update with the simplest, shortest method.) thank you in advance for any help.
rephrasing my question: i'm learning several things from online tutorials, but for some reason i can't find the most basic info about updating a record but NOT based the record's id: for example, you want to update the specific record that matches a specific criteria. (as in the example above, wanting to update whichever record will be at the top of a specific column if that specific column is ordered from top to bottom.)
update main set column5 =
(SELECT item_name FROM main ORDER BY CW DESC)
you can use query like this update and select together your question is not cleared what data you want and which data you want to update so you can search about update select query and make your query as per your requirement....
EUREKA:
$sql = 'UPDATE main
SET testfield="YES" ORDER BY columnX DESC LIMIT 1';
note:the ORDER BY condition did not work with a WHERE line. it worked when i added it to the SET line.
(ftr found code on http://www.phpknowhow.com/mysql/update-statement/)
This part of PHP and SQL is totally new to me where I need to use maths to update a table.
I currently have a table avis_credits with one row only in this table
The table deducts credits as the user uses the system from qty_credits, but i want to be able to add additional credits to his account as per example:
Client has currently 10 credits left he purchases 100 credits so the system must take the current credits and add the 100 to it giving me the 110 credits. I am doing this currently manual adding it my side and updating the table with this expresion.
"UPDATE avis_credits SET credits='$qty_credit'";
What I would like is that the system looks up the current credits and adds the new credits to the row.
I have tried this
"UPDATE avis_credits SET credits='<?php echo qty_credit?> + $qty_credit'";
But it is not working at all. Is it because I am trying to do too many functions in the one string? Or is it because The maths equation is wrong as I have no idea how to write it?
On your first query you're not doing any math you are purely changing it to the value of your variable:
"UPDATE avis_credits SET credits='$qty_credit'";
So credits becomes the value of $qty_credit.
On your second query <?php echo qty_credit?> this is wrong, you're trying to open another php tag on an already open one and you don't use the $ on the variable and then you try to sum the variable so it gives you an error.
"UPDATE avis_credits SET credits='<?php echo qty_credit?> + $qty_credit'";
This will sanitize your input and sum the value of credits with the addition of value of your variable $qty_credit:
$sql = sprintf("UPDATE avis_credits SET credits = credits + '%s'",
mysql_real_escape_string($qty_credit));
As a side note, you may want to specify an ID of the account you want to deposit the additional credit otherwise the above rule will increase that credit for ALL rows.
$sql = sprintf("UPDATE avis_credits SET credits = credits + '%s' WHERE id = '%s'",
mysql_real_escape_string($qty_credit),
mysql_real_escape_string($id));
SQL support arthmetic operations.
So you can do the following:
"UPDATE avis_credits SET credits=credits + $qty_credit";
I have a table in mysql like below-
++++++++++++
+ my_table +
++++++++++++
id
is_done
++++++++++++
And I have a PHP script which performs operation like below-
<?php
$q=mysql_query("SELECT * FROM my_table WHERE is_done=0");
while($res=mysql_fetch_assoc($q)){
$id=$res['id'];
//do something lengthy
sleep(60);
mysql_query("UPDATE my_table SET is_done=1 WHERE id='$id'");
}
?>
Now, if I manually change one row in mysql and set is_done=1 for that row, the script still processes that row. How can I adjust my script to read the fresh and updated row from mysql and skip any row that has been marked as done meanwhile?
why Dont you put the simple thing. what is the purpose of using sleep?
mysql_query("UPDATE my_table SET is_done=1 WHERE is_done=0");
You dont have query in whole table and change one by one.
if you need to use sleep then try this:
mysql_query("UPDATE my_table SET is_done=1 WHERE is_done=0 and id='$id'");
If you are worrying about calling unnecessary sql statements I'd suggest to aggregate the id's to an array and update outside the loop (performance):
<?php
$q=mysql_query("SELECT * FROM my_table WHERE is_done=0");
$ids = array();
while($res=mysql_fetch_assoc($q)){
$ids[]=$res['id'];
//do something lengthy
sleep(60);
}
mysql_query("UPDATE my_table SET is_done=1 WHERE id IN ('".implode("','", $ids)."')");
?>
MySQL won't do anything for rows that are already contains the new data (no affected rows where is_done=1).
If it's the actions you are doing you are worrying about you can use either Transaction to make sure the actions you are doing are Atomic, or Table Locking to make sure no changes are taking place while the script is running.