PHP only adds first row with input while MySQL works fine? -
when click 'add items cart' values on image above, echoing sql query give me this:
insert cart (product_id, quantitycart) values (1, 10) on duplicate key update quantitycart = quantitycart + 10; update products set quantity = quantity - 10 product_id = 1; insert cart (product_id, quantitycart) values (2, 15) on duplicate key update quantitycart = quantitycart + 15; update products set quantity = quantity - 15 product_id = 2; insert cart (product_id, quantitycart) values (3, 20) on duplicate key update quantitycart = quantitycart + 20; update products set quantity = quantity - 20 product_id = 3;
by manually inserting query via phpmyadmin. working fine, inserts 3 queries gives me
1 row affected
now, problem on website when click 'add items cart', inserts first row quantity.
so result give me (it added first row: coca-cola2 value of 10):
this add cart code:
<?php if (isset($_post['addcart']) && $_post['addcart']=="add items cart") { foreach($_post['qtybuy'] $index=>$value){ if($value > 0){ $cartprod_id = $_post['product_id'][$index]; $addquery = "insert cart (product_id, quantitycart) values ($cartprod_id, $value) on duplicate key update quantitycart = quantitycart + $value;"; $addquery .= "update products set quantity = quantity - $value product_id = $cartprod_id;"; $execquery = mysqli_multi_query($connection, $addquery); echo $addquery; } } } ?>
and table of products
<form action="add_sales.php" method="post"> <table class="table table-striped table-bordered table-hover results table-fixed table-condensed"> <thead> <tr> <th class="text-center">#</th> <th>product name</th> <th>description</th> <th>price</th> <th>in stock</th> <th style="width: 20%">quantity</th> </tr> <tr class="warning no-result"> <td colspan="8"><i class="fa fa-warning"></i> no product found</td> </tr> </thead> <tbody> <?php $query = "select * products;"; $exec = mysqli_query($connection, $query); $a = 1; $b = 1; while ($row = mysqli_fetch_array($exec)) { $product_id = $row['product_id']; $product_name = $row['product_name']; $product_price = $row['sell_price']; $description = $row['description']; $product_quantity = $row['quantity']; ?> <tr> <td class="text-center"><?php echo $product_id; ?> <input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>"> </td> <td><?php echo $product_name; ?></td> <td><?php echo $description; ?></td> <td><?php echo $product_price; ?></td> <td><input type="number" value="<?php echo $product_quantity; ?>" id="<?php echo "qtyresult" . $a++; ?>" disabled></td> <td><input type="number" name="qtybuy[]" id="<?php echo "qtybuy" . $b++; ?>" onkeyup="updatestock(this, event)"></td> </tr> <?php } ?> </tbody> </table> </div> <div class="form-group"> <input type="submit" name="addcart" value="add items cart" class="btn btn-info pull-right"> </div> </form>
what problem here? , how insert 3 queries on page?
edit: customer's cart code
<!-- start of customer's cart --> <div class="col-md-12"> <div class="panel panel-default"> <div class="panel-heading"> <strong> <span class="fa fa-shopping-cart"></span> <span>customer's cart</span> </strong> </div> <div class="panel-body"> <table class="table table-hover"> <thead> <tr> <th class="text-center">product id</th> <th class="text-center">product name</th> <th class="text-center">description</th> <th class="text-center">quantity</th> <th class="text-center">price per unit</th> <th class="text-center">total amount</th> <th class="text-center">remove</th> </tr> </thead> <tbody> <?php $selectcart = "select * cart inner join products on products.product_id = cart.product_id"; $execselectcart = mysqli_query($connection, $selectcart); while ($row = mysqli_fetch_array($execselectcart)) { $cartproid = $row['product_id']; $cartproname = $row['product_name']; $cartprodesc = $row['description']; $cartsellprice = $row['sell_price']; $cartqty = $row['quantitycart']; $compute = $cartsellprice * $cartqty; $totalamount = number_format((float)$compute, 2, '.', ''); ?> <tr> <td class="text-center"><?php echo $cartproid; ?></td> <td class="text-center"><?php echo $cartproname; ?></td> <td class="text-center"><?php echo $cartprodesc; ?></td> <td class="text-center"><?php echo $cartqty; ?></td> <td class="text-center"><?php echo $cartsellprice; ?></td> <td class="text-center"><?php echo $totalamount ?></td> <td class="text-center"> <div class="btn-group"> <a href="add_sales.php?remove=<?php echo $cartproid; ?>" class="btn btn-xs btn-danger" data-toggle="tooltip" title="remove"> <span class="glyphicon glyphicon-trash"></span> </a> </div> </td> </tr> <?php } ?> </tbody> </table> </div> </div> <div class="form-group"> <a href="checkout.php" class="btn btn-success pull-right">checkout</a> </div> </div> <!-- end of customer cart -->
edit 2: problem solved. problem mysqli_multi_query
seperated "update products set quantity = quantity - $value product_id = $cartprod_id;";
query.
$addquery = "insert cart (product_id, quantitycart) values ($cartprod_id, $value) on duplicate key update quantitycart = quantitycart + $value;"; $addquery2 = "update products set quantity = quantity - $value product_id = $cartprod_id;"; $execquery = mysqli_query($connection, $addquery); $execquery2 = mysqli_query($connection, $addquery2);
i'll appreciate explanation on why mysqli_multi_query
doesn't work.
you addin 1 or more items cart,so check the $values!==[]
, execute.
Comments
Post a Comment