如果产品ID相同,如何将产品的数量合并为一行?这是我客户的购物车。当我向购物车添加产品时,我希望它组合成一行而不是单独的行。
编辑:我有两个表,其中包含一个单独的数量字段(tblproducts,tblcart)。
EDIT2:PHP给我一个错误
mysqli_fetch_array()期望参数1为mysqli_result boolean 在我的第61行给出
<?php
$query = "SELECT * FROM products";
$exec = mysqli_query($connection, $query);
$a = 1;
$b = 1;
while ($row = mysqli_fetch_array($exec)) {
但我的代码工作正常。有谁能找到问题?
我客户购物车的代码
<table class="table table-hover">
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Description</th>
<th>Quantity</th>
<th>Price per Unit</th>
<th>Total Amount</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<?php
$selectCart = "SELECT * FROM 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><?php echo $cartProId; ?></td>
<td><?php echo $cartProName; ?></td>
<td><?php echo $cartProDesc; ?></td>
<td><?php echo $cartQty; ?></td>
<td><?php echo $cartSellPrice; ?></td>
<td><?php echo $totalAmount ?></td>
<td class="text-center">
<div class="btn-group">
<a href="edit_brand.php?brand_id=<?php echo $brand_id; ?>" class="btn btn-xs btn-info" data-toggle="tooltip" title="Edit">
<span class="glyphicon glyphicon-edit"></span>
</a>
<a href="manage_brands.php?delete=<?php echo $brand_id; ?>" class="btn btn-xs btn-danger" data-toggle="tooltip" title="Remove">
<span class="glyphicon glyphicon-trash"></span>
</a>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
添加产品的代码
<?php
if (isset($_POST['addCart']) && $_POST['addCart']=="Add Items to Cart") {
foreach($_POST['qtyBuy'] as $index=>$value){
if($value > 0){
$cartID = $_POST['product_id'][$index];
$addQuery = "INSERT INTO cart (product_id, quantityCart) VALUES (".$_POST['product_id'][$index].", ".$value.");";
$addQuery .= "UPDATE products SET quantity = quantity - $value WHERE product_id = $cartID;";
$execQuery = mysqli_multi_query($connection, $addQuery);
}
}
header('Refresh: 0; url=add_sales.php');
}
?>
<div class="row">
<div class="col-md-12"> <!-- Product List Info Start -->
<div class="panel panel-default">
<div class="panel-heading">
<strong>
<span class="glyphicon glyphicon-th"></span>
<span>Select Products</span>
</strong>
</div>
<div class="panel-body">
<div class="form-group pull-left">
<input type="text" class="search form-control" placeholder="Search products">
</div>
<span class="counter pull-left"></span>
<div class="span3">
<form action="add_sales.php" method="POST">
<table class="table table-striped table-bordered table-hover results table-fixed">
<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 * FROM 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" name="hehe" 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 to Cart" class="btn btn-info pull-right">
</div>
</form>
表格图片
答案 0 :(得分:0)
如果我理解,您的基本问题是您允许数据库在cart
表中的同一购物车下多次存储相同的产品ID。我的解决方案依赖于同一购物车中所有商品具有相同价值的字段。我使用cart_id
但它可能是其他东西。
由于您希望产品只在同一个购物车中出现一次,因此您应该做两件事:
1。添加UNIQUE INDEX(cart_id, product_id)
cart
表。完成此操作后,任何将同一产品添加到同一购物车的尝试都将失败并显示错误。只运行一次此查询:
ALTER TABLE `cart` ADD UNIQUE INDEX (cart_id, product_id);
2. 更改用于将商品添加到购物车的查询。正如我上面所指出的,如果产品已经在该购物车中,则常规INSERT
将失败。而是使用INSERT...ON DUPLICATE KEY UPDATE...
查询。如果找不到,则此查询将插入产品ID。但是,如果购物车已有该产品,则查询将更新现有记录。
变化:
INSERT INTO cart (product_id, quantityCart)
VALUES (".$_POST['product_id'][$index].", ".$value.")
要:
INSERT INTO cart (cart_id, product_id, quantityCart)
VALUES ($cartID, {$_POST['product_id'][$index]}, $value)
ON DUPLICATE KEY UPDATE quantityCart = quantityCart + $value
我在我的解决方案中使用cart_id
作为确定属于同一购物车的所有商品的方式。如果您有另一个字段执行此操作(例如:user_id
),请使用该字段而不是cart_id
。基本解决方案是一样的。
答案 1 :(得分:0)
假设您有此表
id employee_id contract_id month year d1 d2 d3
1 25 1 11 2011 1 01 01
2 16 5 11 2011 1 11 0
3 29 3 11 2011 1 001 100
1 25 4 11 2011 0 11 011
您希望在Id的基础上合并它,在您的情况下是product_id
id employee_id contract_id month year d1 d2 d3
1 25 1,4 11 2011 1,0 01,11 01,011
2 16 5 11 2011 1 11 0
3 29 3 11 2011 1 001 100
您可以通过此查询
执行此操作SELECT
id,
employee_id,
GROUP_CONCAT(contract_id SEPARATOR ',') AS contract_ids,
`month`,
`year`,
GROUP_CONCAT(d1 SEPARATOR ',') AS d1s,
GROUP_CONCAT(d2 SEPARATOR ',') AS d2s,
GROUP_CONCAT(d3 SEPARATOR ',') AS d3s
FROM
`table`
WHERE
`month` = 11 AND `year` = 2011
GROUP BY
employee_id