php - 在最后一行插入值

标签 php mysql

enter image description here

如上图所示,我的 stock_transfer 表中有两个条目。 作为示例,我将插入两个不同的值来向您展示它的工作原理。 (产品 1 有 5 只股票,产品 2 有 10 只股票) enter image description here

现在,当我尝试插入 Product 1 上剩余的请求库存并保留 Product 2 时,它将成功。 enter image description here

问题来了。当我尝试在我的 Product 2 上插入值(290 支股票)时,它会将值插入到 Product 1 表中。 enter image description here 谁能帮我这个?这是我的查询代码:

 if (isset($_POST['addCart']) && $_POST['addCart']=="Confirm Stock Transfer") {
 foreach($_POST['qtyBuy'] as $index=>$value){
 if($value > 0){

  $cartProd_id = $_POST['product_id'][$index];
  $cartProd_name = $_POST['product_name'][$index];
  $cartProd_date = $_POST['product_exp'][$index];
  $cartProd_desc = $_POST['product_desc'][$index];
  $cartProd_transid = $_POST['transfer_id'][$index];

  //Update stock_transfer requests
  $update_stock = "UPDATE stock_transfer SET stocks_transferred = stocks_transferred + $value WHERE transfer_id = $cartProd_transid;";
  $update_stockE = mysqli_query($connection, $update_stock);

这是我的代码:

<table class="table table-striped table-bordered table-hover results table-fixed table-condensed" id="example">
    <thead>
      <tr>
        <th>#</th>
        <th>Product Name</th>
        <th>Description</th>
        <th>Sell Price</th>
        <th>Expiry Date</th>
        <th>Instock</th>
        <th>Stocks Requested Remaining</th>
        <th>Stocks Transferred</th>
        <th>Quantity to Transfer</th>
      </tr>
    </thead>
    <tbody>
    <?php 

      $getReq = "SELECT * FROM stock_transfer INNER JOIN td_products ON stock_transfer.transfer_product_id = td_products.product_id WHERE stock_transfer.transfer_number = $tid AND stock_transfer.transfer_tobranch = '$capitalPrefix';";
      $getReqE = mysqli_query($connection, $getReq);

      while ($row = mysqli_fetch_array($getReqE)) {
        $transfer_id = $row['transfer_id'];
        $transfer_number = $row['transfer_number'];
        $transfer_status = $row['transfer_status'];
        $transfer_frombranch = $row['transfer_frombranch'];
        $transfer_tobranch = $row['transfer_tobranch'];
        $transfer_product_id = $row['transfer_product_id'];
        $transfer_quantity = $row['transfer_quantity'];
        $transfer_date = $row['transfer_date'];
        $stocks_transferred = $row['stocks_transferred'];

        $remainingStocks = $transfer_quantity - $stocks_transferred;

        $product_id = $row['product_id'];
        $product_name = $row['product_name'];
        $product_price = $row['sell_price'];
        $description = $row['description'];
        $quantity = $row['quantity'];
        $expiry_date = $row['expiry_date'];
        echo $transfer_id;

     ?>
        <tr>
          <td class="text-center"><?php echo $product_id; ?>
            <input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
          </td>
            <input type="hidden" name="transfer_id[]" value="<?php echo $transfer_id; ?>">
          <td><?php echo $product_name; ?>
            <input type="hidden" name="product_name[]" value="<?php echo $product_name; ?>">
          </td>
          <td><?php echo $description; ?>
            <input type="hidden" name="product_desc[]" value="<?php echo $description; ?>">
          </td>
          <td>₱ <?php echo number_format($product_price, 2); ?></td>
          <td><?php echo $expiry_date; ?>
            <input type="hidden" name="product_exp[]" value="<?php echo $expiry_date; ?>">
          </td>
          <td><strong><?php echo $quantity; ?></strong></td>
          <td><?php echo $remainingStocks; ?></td>
          <td><?php echo $stocks_transferred; ?></td>
          <td>
            <?php if (!$remainingStocks == 0){ ?>
            <input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>">
            <?php } else{ ?>
              <i class="glyphicon glyphicon-check"></i> Completed
            <?php } ?>
          </td>
        </tr>
        <?php }?>
      </tbody>
    </table>

    <div class="form-group">
      <input type="submit" name="addCart" value="Confirm Stock Transfer" class="btn btn-info pull-right">
      <a href="stock_manage_requests.php" class="btn btn-warning pull-left">Back to Request List</a>
    </div>

标签

这是我的架构: enter image description here

最佳答案

看这个:

  <td>
     <input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>" <?php echo ($remainingStocks == 0') ? 'Disabled=true' : '';?> value="0">
   </td>

关于php - 在最后一行插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40836900/

相关文章:

PHP cURL HTTP 代码返回 0

mysql - 我怎么知道什么时候该切换到执行从 MySQL 查询到 Redis 的读取操作?

javascript - 使用ajax通过javascript提示验证密码

mysql - 为什么在鼓励使用InnoDB 的情况下,MySQL mysql 表仍然是MYISAM?

javascript - 如何正确地将base64编码的图像上传到文件服务器?

php - 重置现有表中的 id 值(不删除记录)

PHP mysqli 查询不返回任何结果

php - 生产服务器上需要哪些 Laravel Elixir 文件?

mysql - 在mysql中按月和年比较日期

mysql - 如何在 SQL 中获取用户的当前交易详细信息?