我在添加价格,尺寸和库存时遇到问题。如何在不重复的情况下添加价格,库存和尺寸?
这是我的数据库结构:
tblproduct
tblextension
PS:tblproduct product_id
和tblproduct_extension product_id
是相同的。
这是我添加价格,股票和价格后的结果。如你所见,有两个'Nike Lebron 15 Ghost'。
查询
SELECT * FROM tblproduct LEFT JOIN tblproduct_extension ON tblproduct_extension.product_id = tblproduct.product_id;
AJAX
//Show modal for adding size, price and stocks
function add_product_extension() {
$('#product_add_extension').click(function(e){
var product_id = $('#product_id').val();
var product_price = $('#product_price').val();
var product_stocks = $('#product_stocks').val();
var product_size = $('#product_size').val();
$.ajax({
type: 'POST',
url: '../admin/class.php',
data: {product_id:product_id,product_price:product_price,product_stocks:product_stocks,product_size:product_size},
success:function(data) {
$('#addSize').modal('hide');
}
});
});
}
class.php
//Add size, price, stocks
if(isset($_POST['product_id'])) {
$product_price = $_POST['product_price'];
$product_stocks = $_POST['product_stocks'];
$product_size = $_POST['product_size'];
$product_id = $_POST['product_id'];
$insert_query = "INSERT INTO tblproduct_extension (product_stocks, product_price, product_size, product_id)VALUES('$product_stocks', '$product_price', '$product_size', '$product_id')";
$query = mysqli_query($db_conn, $insert_query);
}
显示产品的代码
//Get Products by ID
function get_products() {
GLOBAL $db_conn;
$id = $_GET['id'];
$search_query= "SELECT * FROM tblproduct LEFT JOIN tblproduct_extension ON tblproduct_extension.product_id = tblproduct.product_id WHERE tblproduct.segment_id='$id'";
$query = mysqli_query($db_conn, $search_query);
while ($row = mysqli_fetch_array($query)) {
?>
<div class="products">
<a href="details.php?view_product=<?=$row['product_id']?>&id=<?=$row['segment_id']?>"><img class="img_product" src="../admin/<?=$row['product_image']?>" alt="<?=$row['product_name']?>"></a>
<?php if($row['product_stocks'] == 0) {?>
<span class="stock-label">SOLD OUT</span>
<?php } ?>
<h4><?=$row['product_name']?></h4>
<h3>₱<?=number_format($row['product_price'], 2)?></h3>
</div>
<?php
}
return $row;
}
答案 0 :(得分:2)
$search_query= "SELECT DISTINCT * FROM tblproduct LEFT JOIN tblproduct_extension ON tblproduct_extension.product_id = tblproduct.product_id WHERE tblproduct.segment_id='$id'";
SELECT DISTINCT语句用于仅返回不同的(不同的)值。
如果您不想复制,可以从查询中修剪它们。
答案 1 :(得分:1)
由于您可以拥有多个product_size,因此您还可以使用产品名称
显示此信息...
<h4><?=$row['product_name']?> - Size <?=$row['product_size']?></h4>
...
你可以做一些更复杂的查询来实现你想要的(函数get_products)。
首先选择唯一的产品/扩展名,扩展名最小。从这里,您进行第二次选择以获取要显示的数据。
这些查询未经过测试,但提出了一个想法。
使用GROUP BY
-- 2. After you select your data with unique id get from sub query
SELECT p.*, pe.*
FROM tblproduct p
JOIN (
-- 1. First select your product_id and smallest product_extension_id
SELECT p.product_id,
MIN(pe.product_extension_id) as product_extension_id
FROM tblproduct p
LEFT JOIN tblproduct_extension pe
ON pe.product_id = p.product_id
WHERE p.segment_id='$id'
GROUP BY p.product_id
) product_unique
JOIN tblproduct_extension pe
ON pe.product_extension_id = product_unique.product_extension_id
WHERE p.product_id = product_unique.product_id
使用DISTINCT
-- 2. After you select your data with unique id get from sub query
SELECT p.*, pe.*
FROM tblproduct p
JOIN (
-- 1. First select your product_id and smallest product_extension_id
SELECT DISTINCT
p.product_id,
MIN(pe.product_extension_id) as product_extension_id
FROM tblproduct p
LEFT JOIN tblproduct_extension pe
ON pe.product_id = p.product_id
WHERE p.segment_id='$id'
) product_unique
JOIN tblproduct_extension pe
ON pe.product_extension_id = product_unique.product_extension_id
WHERE p.product_id = product_unique.product_id