Update MySQL data based on radio button - php

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

Related

Any idea why my query only works on first but on second isset nothings happen

if (isset($_POST['submitid'])) {
$itemid = $_POST['itemID'];
$cartno = mysqli_query($connection , "SELECT * FROM users");
while ($cartnorow = mysqli_fetch_assoc($cartno)) {
$existingcartno = $cartnorow['cartno'];
$existingtotal = $cartnorow['total'];
}
$updatecartno = $existingcartno + 1;
$updateprice = $itemlistprice+$existingcartno;
mysqli_query($connection ,
"UPDATE users SET cartno = '$updatecartno' WHERE id=1");
}
When I remove WHERE id = 1 it works fine.
I seriously need to update specific id thats why I need that WHERE id = 1.

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.

Query updating all records rather than record which matches input?

Apologies for vague title.
I currently have an edit form in which a product can be selected and the details are displayed in a form where they can be edited. Unfortunately, when edited every product in the product table is edited rather than just the product selected. To select the product i'm using where productname = productnameinput etc etc (Code below)
Database: http://prnt.sc/f4tf5g (Before an edit)
Tried adding the following WHERE statements at the end of the update query:
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE ProductName = :prodname
PHP:
// edit product in database
$query="
SELECT * FROM category
";
$result = $DBH->prepare($query);
$result->execute();
$categories = $result->fetchAll();
//we need to select all products frist
$query3 = "
SELECT * FROM product
";
$result3 = $DBH->prepare($query3);
$result3->execute();
$allProducts = $result3->fetchAll();
//When the Product is selected this function is run
if (isset($_POST['choose'])) {
$query2 = "
SELECT product.*, category.* FROM product LEFT JOIN category ON category.CategoryID = product.CategoryID WHERE product.ProductName = :prodname
";
$result2 = $DBH->prepare($query2);
$result2->bindParam(':prodname', $_POST['product_name']);
$result2->execute();
$product = $result2->fetch();
}
//When the Update button is Pressed
if (isset($_POST['update'])) {
$query4 = "
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname
";
$result4 = $DBH->prepare($query4);
$result4->bindParam(':newCatId', $_POST['newcategory']);
$result4->bindParam(':newProdName', $_POST['productName']);
$result4->bindParam(':newProdDesc', $_POST['productDescription']);
$result4->bindParam(':newStock', $_POST['stockCount']);
$result4->bindParam(':prodname', $_POST['product_name']);
$result4->execute();
}
You are updating entire table, there is not filter in your update
$query4 = "
UPDATE product SET CategoryID = :newCatId, ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock
";

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.

Issue updating values in Database from mySQL query on PHP site

Been tinkering with my website, it is a seat booking website. Still in alpha testing really so not live to the public yet for obvious reasons.
However, I'm having a few problems with updating the values in my database.
I'll post the code and then explain the problem..
else {
$seatID = $_POST['form_submitted'];
$query1 = "SELECT seatTaken FROM SEATS WHERE seatNo = '$seatID'";
$result = mysql_query($query1);
while($row = mysql_fetch_array($result))
{
$taken = $row['seatTaken'];
}
$query2 = "SELECT passNo FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query2);
while($row = mysql_fetch_array($result))
{
$passno = $row['passNo'];
}
$query3 = "SELECT groupID FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query3);
while($row = mysql_fetch_array($result))
{
$groupno = $row['groupID'];
}
$query4 = "SELECT flightNo FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query3);
while($row = mysql_fetch_array($result))
{
$flightno = $row['flightNo'];
}
// if ($taken = 0) {
$update = mysql_query("UPDATE PASSENGER SET seatNo = $seatID WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passNo', groupID = '$groupid' WHERE seatNo = '$seatID'");
// AND flightNo = '$flightno'"
echo '<meta http-equiv="refresh" content="5;url=http://www.mywebsite.com/">';
echo mysql_error();
//}
}
?>
Now the user will have selected their seat in the previous form hence the:
$seatID = $_POST['form_submitted'];
However, at the bottom in my queries, the only value that actually changes in the database when this PHP code is run is the boolean value of 'seatTaken', in that it does change from 0 (not occupied) to 1 (occupied).
The field passNo and groupID in my database DO NOT UPDATE as referenced here in these queries:-
$update = mysql_query("UPDATE PASSENGER SET seatNo = $seatID WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passNo', groupID = '$groupid' WHERE seatNo = '$seatID'");
Is anyone able to help? Many thanks!
Tom
Watch your variable naming and string quotation
When your looking for values in mysql, they usually need to be a string literal (add quotes).
And your other problem is your variable names:
$update = mysql_query("UPDATE PASSENGER SET seatNo = '$seatID' WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passno', groupID = '$groupno' WHERE seatNo = '$seatID'");
$passno vs $passNo
$groupid vs $groupno
You should also make sure you properly escape any input coming from the user http://php.net/manual/en/function.mysql-real-escape-string.php
One can't see in your code how do you generate the values of $groupid, $passNo, $seatID. Are those varaibles set when you do your update? (just echo the SQL code to see what query is being sent to your database)
Maybe you should try getting the variables from your post request, like $_POST['groupid'], if groupid is the name of the field in the form.

Categories