PHP: MySQL: Update is Updating More than One Row - php

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.

Related

Update MySQL data based on radio button

I am facing a challenge right now.
I have a return page, if I selected the first row via the radio button, I would like to change the history_status inside my order history table from "DONE" to "RETURN PENDING". The reason why I am doing this is to run a query to show only history_status value with "DONE".
Below are the code I am working with:
<?php
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
$FBdate = date("Y-m-d H:i:s");
$sql = "SELECT p.product_id ,p.product_name , p.price, p.product_description, o.history_id ,o.qty , o.subtotal, o.history_datetime , o.history_status FROM product p, orderhistory o where o.product_id = p.product_id AND o.history_status = 'DONE'";
$result=mysqli_query($db,$sql);
$Cntsql = "SELECT count(return_id) AS total FROM retrn";
$res = mysqli_query($db,$Cntsql);
$value = mysqli_fetch_assoc($res);
$num = $value['total'];
if (isset($_POST['submitbuttonform']))
{
$return_id = $num+1;
$return_status = 'PENDING';
$return_reason = $_POST['reasonselected'];
$return_option = $_POST['returnoption'];
$return_datetime = $FBdate;
$history_id = $_POST['selectitemradio'];
$history_status = "UPDATE orderhistory SET history_status = 'RETURN PENDING'";
$sql = "INSERT INTO `retrn`(`return_id`, `return_status`, `return_reason`, `return_option`, `return_datetime`, `history_id`) VALUES ('$return_id','$return_status','$return_reason','$return_option','$return_datetime','$history_id')";
$sql = "INSERT INTO 'orderhistory'('history_status') VALUES ('$history_status')";
$result=mysqli_query($db,$sql);
Order History Table
Return page layout
From your code
$history_id = $_POST['selectitemradio']; I assume you get the history id from the page.
So you can use the history_id in the where clause. Now only the selected row will be updated.
UPDATE orderhistory SET history_status = 'RETURN PENDING' where history_id=$history_id

I want in this code please to increase the quantity of same item when insert without add new record to the table

I want in this code please to increase the quantity of the same item when inserting
without adding a new record to the table.
the insert working is done, but please I need when inserting to the table that
contains a unique number of the invoice to increase the number of items without
duplicate records.
<?php
session_start();
include('../connect.php');
$a = $_POST['invoice'];
$b = $_POST['product_id'];
$c = $_POST['qty'];
$saleproduct = $_POST['saleproduct'];
$w = $_POST['pt'];
$amount = $_POST['amount'];
$dateok = $_POST['dateok'];
$result = $db->prepare("SELECT * FROM products WHERE product_id= :userid");
$result->bindParam(':userid', $b);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$sellprice=$row['price'];
$prodcode=$row['prod_code'];
$code=$row['product_code'];
$color=$row['color_name'];
$size=$row['size_name'];
$sizenum=$row['note'];
$name=$row['product_name'];
}
$sql = "UPDATE products
SET qty=qty-?
WHERE product_id=?";
$q = $db->prepare($sql);
$q->execute(array($c,$b));
$grandtotal = $sellprice- $saleproduct ;
$d=$grandtotal*$c;
$sql = "INSERT INTO sales_order
(invoice_number,prod_code,product_id,qty,amount,saleproduct,name,color_name,size_name,note,price,product_code,date,dateok) VALUES
(:a,:prodcode,:b,:c,:d,:saleproduct,:e,:ee,:eee,:sizenum,:sellp,:i,:k,:kok)";
$q = $db->prepare($sql);
$q->execute(array(':a'=>$a,':prodcode'=>$prodcode,':b'=>$b,':c'=>$c,':d'=>$d,':saleproduct'=>$saleproduct,':e'=>$name,':ee'=>$color,':eee'=>$size,':sizenum'=>$sizenum,':sellp'=>$sellprice,':i'=>$code,':k'=>$date,':kok'=>$dateok));
?>
You'll need to run UPDATE query to update quantity of the existing record i.e.:
UPDATE `sales_order` SET `qty` = `qty` + 1 WHERE `id` = ?
You will obviously have to bind id of the desired record within sales_order table.

Updating MYSQL table row column sum by appending

I would like to update a column row inside MYSQL by adding 50 to the last current number.Means column named sum was 200 and after query, it will update to 250.
I can do this by using two query as follow:
$add1 = 50;
$sql = "SELECT sum FROM table_name WHERE id = '$id' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);
$add2 = $row['sum'] + $add1 ;
}
$sql1 = "UPDATE tabel_name SET sum = '$add2' WHERE id = '$id' ";
$result1 = $conn->query($sql1);
However, is there a better way to do this with less code or by one go?
Thanks
You can calculate values directly in UPDATE queries.
$add1 = 50;
$sql1 = "UPDATE tabel_name SET sum = sum + $add1 WHERE id = '$id';";
In case the type of your column sum is not numeric, but contians a string of the given number, you can use the MySQL CAST function to cast it to a number, calculate it and cast it back to a string again:
$sql1 = "UPDATE tabel_name SET sum = CAST((CAST(sum as INT(11)) + $add1) as VARCHAR(11)) WHERE id = '$id';";
Why dont you try By
$sql1 = "UPDATE tabel_name SET sum = sum + 'YOUR VALUE' WHERE id = '$id'
You can do it using UPDATE for this, just change your query like:
$sql = "UPDATE table_name SET sum=sum+value_you_want WHERE id = '$id' ";
Please use this sql query:
$sql1 = "UPDATE tabel_name SET sum = sum + 50 WHERE id = '$id' ";

SQL Updates: Updates Multiple rows rather than Single rows

I am writing a code for MySQL to fetch the 1st row with status is "inactive" and make them "active", but whenever I tried to update the column and make it "active" my query updates multiple rows rather than the single row.
date_default_timezone_set('Asia/Kolkata');
$d = time ();
$date = date("Y-m-d", $d);
$customer_id="1470831854";
$member_details="SELECT * FROM login_update WHERE customer_id ='$customer_id' AND status='inactive' ORDER BY id ASC LIMIT 1 ";
$member = mysql_query($member_details);
while($list = mysql_fetch_array($member)){
$status = $list['status'];
$id = (int)$list['id'];
}
$date_update = "UPDATE login_update SET status='active' WHERE id = '$id'";
$enter_date = mysql_query($date_update);
I think your code should be change as follow
while($list = mysql_fetch_array($member)){
$status = $list['status'];
$customerId = (int)$list['customer_id'];
}
$date_update = "UPDATE login_update SET status='active' WHERE id = '$customerId'";
$enter_date = mysql_query($date_update);
becasue if you get $list['id'] it is always return only 1 unique value from the database then update only one record. I assumed id is your primary key.

MSSQL get INSERTED ID and return it with string PHP

Hello I have working mssql php insert codes but i want to get inserted id return with string ($newid) My codes under.
OrderId = int and Primary Key not null
$querysf = "SELECT TOP 1 OrderId FROM Orders ORDER BY OrderId DESC";
$resultsf = mssql_query($querysf);
//Getting manually latest OrderId but this is not good :(
while($rowsf = mssql_fetch_array($resultsf))
{
$newid = $rowsf["OrderId"];
$newid = $newid + 1;
echo("$newid"); //Getting manually latest OrderId but this is not good :( Some one can easily take this id and will be mistakes
$mydate = date("Y-m-d");
$ready = mssql_fetch_assoc(mssql_query("SET IDENTITY_INSERT Orders ON; SELECT CONVERT(NCHAR(100),NEWID()) as Ok"));
$queryer = mssql_query("INSERT INTO Orders (OrderId, CustomerId, DeliveryDate, OrderState,Signature,UserName,special_field,rowguid) VALUES ('$newid', '$CustomerId', '$mydate 00:00:00.000', '1',NULL,'$UserName',NULL,'".$ready['Ok']."')");
$close = mssql_fetch_assoc(mssql_query("SET IDENTITY_INSERT Orders OFF;"));
}

Categories