【发布时间】:2018-02-17 13:52:49
【问题描述】:
我一直在从事库存管理项目,一切都很好,直到我遇到这种情况,我需要告诉 PHP 从数据库中存在的数量数字中减去行中给出的数量数字。但总是出现问题并阻止脚本工作。
如果我有 20 件 T 恤,我订购了 2 件,所以 php 很好地更新了数据库并将数量设置为 18。
这是我的代码:
<?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;
?>
【问题讨论】: