Php 在最后一行插入值

Php 在最后一行插入值,php,mysql,Php,Mysql,如上图所示,我的stock\u transfer表中有两个条目。 作为一个例子,我将插入两个不同的值来向您显示它正在工作。(产品1的5只股票和产品2的10只股票) 现在,当我尝试在产品1上插入请求的剩余库存,并离开产品2时,它将成功。 问题来了。当我尝试在我的产品2上插入值(290只股票)时,它将在产品1表中插入值。 有人能帮我吗?这是我的查询代码: if (isset($_POST['addCart']) && $_POST['addCart']=="Confirm St

如上图所示,我的
stock\u transfer
表中有两个条目。 作为一个例子,我将插入两个不同的值来向您显示它正在工作。(产品1的5只股票和产品2的10只股票)

现在,当我尝试在
产品1
上插入请求的剩余库存,并离开
产品2
时,它将成功。

问题来了。当我尝试在我的
产品2
上插入值(290只股票)时,它将在
产品1
表中插入值。 有人能帮我吗?这是我的查询代码:

 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>

#
品名
描述
售价
到期日
因斯托克
所需库存剩余
转让的股票
要转移的数量

我认为问题在于你输入的名字。您已将数组指定为输入的名称。尝试使用产品id使其唯一。

请参见:

  <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 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 } ?>


对于安全问题,我建议您使用ajax,每次只提交一行的数量输入:

<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; ?>
      </td>
      <td><?php echo $product_name; ?>
      </td>
      <td><?php echo $description; ?>
      </td>
      <td>₱ <?php echo number_format($product_price, 2); ?></td>
      <td><?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="qty_buy_<?= $transfer_id ?>" min="1" max="<?php echo $remainingStocks;?>"><a class="btn btn-sm add_qty" data-id="<?= $transfer_id ?>"></a>
        <?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>

<script>

$(function() {

    $('.add_qty').click(function() {
        var id = $(this).data('id');
        var qty = $("#qty_buy_"+id).val();

        $.ajax({
            type: 'GET',
            url:'/your_action',
            data: {id : id, qty : qty},
            dataType: 'JSON',
        }).done(function(json){
            // code to update the values in your row
        });
    });
});

</script>

#
品名
描述
售价
到期日
因斯托克
所需库存剩余
转让的股票
要转移的数量
₱ 


echo“UPDATE stock\u transfer SET stocks\u transfer=stocks\u transfer+$value,其中transfer\u id=$cartProd\u transid”
并查看您是否获得了产品的正确
id
?对于这两种情况,@BarneyStinson
更新库存转移集库存转移=库存转移+10,其中转移id=2
它第二次给了我正确的
id
。请将这两个案例都回显并粘贴到这里。我不知道你的更新查询是怎么做的properly@BarneyStinson
更新库存转移集库存转移=库存转移+300,其中转移id=1这是在完成对
产品1
@BarneyStinson
的请求后,第一次在
产品2
上插入值,其中transfer\u id=1
是我的
产品2
的查询结果,你能给我举个例子吗?