Odie
Odie

Reputation: 375

Value inserting on last row

enter image description here

Shown in the image above, I have two entries on my stock_transfer table. As an example, I will insert two different values to show you that it's working. (5 stocks on Product 1 and 10 stocks on Product 2) enter image description here

Now, when I try to insert the stocks requested remaining on Product 1 and leave the Product 2, it will be successful. enter image description here

Here comes the problem. When I try to insert value (290 stocks) on my Product 2, it will insert the value in the Product 1 table. enter image description here Can anyone help me with this? Here's my query code:

 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);

Here's is my code:

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

Tags

Here's my schema: enter image description here

Upvotes: 3

Views: 76

Answers (4)

ahammar
ahammar

Reputation: 292

for security issues, I advise you to use ajax and submit just the qty input for one row each time:

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

Upvotes: 1

Odie
Odie

Reputation: 375

The problem was on the

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

Upvotes: 0

Akshay
Akshay

Reputation: 710

See this:

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

Upvotes: 1

BlackDeath
BlackDeath

Reputation: 19

I think the problem is with your input names. You have assigned arrays as names of your inputs. Try to make them unique using the product id.

Upvotes: -1

Related Questions