【问题标题】:Update stock using PHP and MySQL使用 PHP 和 MySQL 更新库存
【发布时间】:2020-01-16 20:21:32
【问题描述】:

我目前正在为大学项目开发​​电子商务网站,并且我正在尝试在下订单后减少库存。无法确定将引用产品表中“库存”行的代码行放在哪里。 我将在此处添加“action.php”代码。 很抱歉代码太长,不知道您需要什么帮助。

<?php  
    session_start();
    require 'config.php';
    if(isset($_POST['pid'])){

        $pid = $_POST['pid'];
        $pname = $_POST['pname'];
        $pprice = $_POST['pprice'];
        $pimage = $_POST['pimage'];
        $pcode = $_POST['pcode'];
        $pqty = 1;

        $stmt = $conn->prepare("SELECT product_code FROM cart WHERE product_code=?");
        $stmt->bind_param("s",$pcode);
        $stmt->execute();
        $res = $stmt->get_result();
        $r = $res->fetch_assoc();
        $code = $r['product_code'];

        if(!$code){
            $query = $conn->prepare("INSERT INTO cart (product_name,product_price,product_image,qty,total_price,product_code) VALUES (?,?,?,?,?,?)");
            $query->bind_param("sssiss",$pname,$pprice,$pimage,$pqty,$pprice,$pcode);
            $query->execute();



            echo '<div class="alert alert-success alert-dismissible mt-2">
                          <button type="button" class="close" data-dismiss="alert">&times;</button>
                          <strong>Item added to your cart!</strong>
                        </div>';
        }
        else{
            echo '<div class="alert alert-danger alert-dismissible mt-2">
                          <button type="button" class="close" data-dismiss="alert">&times;</button>
                          <strong>Item already added to your cart!</strong>
                        </div>';
        }
    }

    if(isset($_GET['cartItem']) && isset($_GET['cartItem']) == 'cart_item'){
        $stmt = $conn->prepare("SELECT * FROM cart");
        $stmt->execute();
        $stmt->store_result();
        $rows = $stmt->num_rows;

        echo $rows;
    }

    if(isset($_GET['remove'])){
        $id = $_GET['remove'];

        $stmt = $conn->prepare("DELETE FROM cart WHERE id=?");
        $stmt->bind_param("i",$id);
        $stmt->execute();

        $_SESSION['showAlert'] = 'block';
        $_SESSION['message'] = 'Item removed from the cart!';
        header('location:cart.php');
    }

    if(isset($_GET['clear'])){
        $stmt = $conn->prepare("DELETE FROM cart");
        $stmt->execute();
        $_SESSION['showAlert'] = 'block';
        $_SESSION['message'] = 'All Item removed from the cart!';
        header('location:cart.php');
    }

    if(isset($_POST['qty'])){
        $qty = $_POST['qty'];
        $pid = $_POST['pid'];
        $pprice = $_POST['pprice'];

        $tprice = $qty*$pprice;

        $stmt = $conn->prepare("UPDATE cart SET qty=?, total_price=? WHERE id=?");
        $stmt->bind_param("isi",$qty,$tprice,$pid);
        $stmt->execute();
    }

这里有“cart.php”代码,如果你还需要什么,请告诉我。

 <?php  
                require 'config.php';
                $stmt = $conn->prepare("SELECT * FROM cart");
                $stmt->execute();
                $result = $stmt->get_result();
                $grand_total = 0;
                while($row = $result->fetch_assoc()):
              ?>
              <tr>
                <td><?= $row['id'] ?></td>
                <input type="hidden" class="pid" value="<?= $row['id'] ?>">
                <td><img src="<?= $row['product_image'] ?>" width="50"></td>
                <td><?= $row['product_name'] ?></td>
                <td>
                  <i class="fas fa-dollar-sign"></i>&nbsp;&nbsp;<?= number_format($row['product_price'],2); ?>
                </td>
                <input type="hidden" class="pprice" value="<?= $row['product_price'] ?>">
                <td>
                  <input type="number" class="form-control itemQty" value="<?= $row['qty'] ?>" style="width:75px;">
                </td>
                <td><i class="fas fa-dollar-sign"></i>&nbsp;&nbsp;<?= number_format($row['total_price'],2); ?></td>
                <td>
                  <a href="action.php?remove=<?= $row['id'] ?>" class="text-danger lead" onclick="return confirm('Are you sure want to remove this item?');"><i class="fas fa-trash-alt"></i></a>
                </td>
              </tr>
              <?php $grand_total +=$row['total_price']; ?>
            <?php endwhile; ?>

【问题讨论】:

  • PHPMyAdmin 不是数据库。它是 MySQL 数据库的 Web 界面。
  • 是的,对不起..我的错。
  • 我改了标题,你能看一下代码吗?
  • 我只看到处理购物车的代码。库存是什么意思?
  • 这里有你需要的所有代码:action.php:pastebin.com/4naXb14Sindex.php:pastebin.com/zVnqcWutcart.php:pastebin.com/SYHJVVgL

标签: php mysqli shopping-cart


【解决方案1】:

您发布的代码仅处理购物车表。您也没有显示表格的结构。我假设您有另一个处理产品数量的表。假设表名是产品,您可以获得订购的每个产品的数量。以下是如何更新库存以及我如何在我自己的项目中更新它。

$stmt = $conn->prepare("UPDATE product SET product_quantity = product_quantity-? WHERE product_id = ?   ");
        $stmt->bind_param("ii",$_POST['qty'],$_POST['pid']);
        $stmt->execute();

如果您添加库存

$stmt = $conn->prepare("UPDATE product SET product_quantity = product_quantity+? WHERE product_id = ?   ");
        $stmt->bind_param("ii",$_POST['qty'],$_POST['pid']);
        $stmt->execute();

你可以直接告诉mysql帮你计算。你只需要告诉数量,id和操作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    相关资源
    最近更新 更多