I've been working on stock management project, everything was okay until I get in this situation where I need to tell PHP to Subtract the amount number which has been given in the row from the amount number which exists in the database. but something went wrong always and stopping the script from working .
Like if I have 20 T-shirt and I ordered 2, so php well went update the database and set the amount to 18.
Here is my code:
<?php
require_once 'connections/dbc.php';
$user_name= mysqli_real_escape_string($conn, $_POST['user_name']);
$brand_name= mysqli_real_escape_string($conn, $_POST['brand_name']);
$product_name= mysqli_real_escape_string($conn, $_POST['product_name']);
$amount = mysqli_real_escape_string($conn, $_POST['amount']);
$discount= mysqli_real_escape_string($conn, $_POST['discount']);
$total_preis= mysqli_real_escape_string($conn, $_POST['total_preis']);
$payment_type= mysqli_real_escape_string($conn, $_POST['payment_type']);
if(isset($_POST['orderbutton'])):
$sql = "INSERT INTO `orders` (`order_id`, `order_date`, `user_name`, `brand_name`, `product_name`, `amount`, `discount`, `total_preis`, `payment_type`) VALUES ('', 'NOW()','$user_name','$brand_name','$product_name','$amount','$discount', '$total_preis', '$payment_type')";
for($x = 0; $x < count($_POST['product_name']); $x++) {
$updateProductAmountSql = "SELECT product.amount FROM product WHERE product.product_name = ".$_POST['product_name'][$x]."";
$updateProductAmountData = $connect->query($updateProductAmountSql);
while ($updateProductAmountResult = $updateProductAmountData->fetch_row()) {
$updateAmount[$x] = $updateProductAmountResult[0] - $_POST['amount'][$x];
// update product table
$updateProductTable = "UPDATE product SET amount = '".$updateAmount[$x]."' WHERE product_name = ".$_POST['product_name'][$x]."";
$connect = query($updateProductTable);
} // while
}// /for amount
endif;
?>