简体   繁体   中英

PHP: MySQL: Update is Updating More than One Row

So I am trying to update update my 2 tables: Invoice and Order_Table. I want it to update the new quantity and the new price in each table but only for the specific OrderID. I came up with this:

$UpdateQuant = "UPDATE Order_Table SET Quantity = '$NewQuant' WHERE OrderID = '$OrderID' ";
$UpdateQuant = mysql_query($UpdateQuant);

$UpdatePrice = "UPDATE Order_Table SET TotalCost = '$NewPrice' WHERE OrderID = '$OrderID' ";
$UpdatePrice = mysql_query($UpdatePrice);


//Update Invoice Table
$UpdateQuant = "UPDATE Invoice SET Quantity = '$NewQuant' WHERE OrderID = '$OrderID' ";
$UpdateQuant = mysql_query($UpdateQuant);                   

$UpdatePrice = "UPDATE Invoice SET TotalCost = '$NewPrice' WHERE OrderID = '$OrderID' ";
$UpdatePrice = mysql_query($UpdatePrice);

However, when I execute this, it updates every single row. I don't see why this would even happen since I'm using WHERE OrderID = '$OrderID'

Echo out the SQL just before you execute it, maybe something isn't in the variables as you expect. Otherwise, it would have to be that each record updated has the same orderID.

Also, as pointed out int he comments, only strings should be encapsulated by quotes, numeric types shouldn't have them:

You can also combine the queries you run into a single one like this:

$UpdateQuant = "UPDATE Order_Table SET Quantity = $NewQuant, TotalCost = $NewPrice WHERE OrderID = $OrderID ";
$UpdateQuant = mysql_query($UpdateQuant);

Which has combined the top two queries together and you can do likewise for the bottom two.

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