【问题标题】:How to tell php to count the data and +/- it from the data base to have the new number如何告诉 php 计算数据并从数据库中对其进行 +/- 以获得新数字
【发布时间】: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;
?>

【问题讨论】:

    标签: php html mysql sql


    【解决方案1】:

    您的查询和周围的代码是……嗯,有点乱<_>

    去掉你的For/while,你只需要从stock中减去ordered值:

    $sql2 = "UPDATE product 
        SET amount = amount - ".$amount." 
        WHERE product_name = '".$product_name."'";
    

    【讨论】:

    • 非常感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多