Php 订单成功后的库存管理
Managament of Stock After Order Success in Php
我正在从事库存商店项目。一切顺利,但我在订单成功后遇到了问题。
当用户下订单并重定向到结帐 Page.When 时,用户填写帐单并单击按钮。我 运行 在数据库中存储订单及其详细信息的过程以及在数据库中管理产品库存的过程。
来自隐藏字段的值:
<?php
foreach ($_SESSION['shopping_cart'] as $key => $value) {
?>
<input type="hidden" name="product_id[]" value="<?php echo $value['product_id'] ?>">
<input type="hidden" name="product_name[]" value="<?php echo $value['product_name'] ?>">
<input type="hidden" name="product_color[]" value="<?php echo $value['product_color'] ?>">
<input type="hidden" name="product_size[]" value="<?php echo $value['product_size'] ?>">
<input type="hidden" name="product_quantity[]" value="<?php echo $value['product_quantity'] ?>">
<?php } ?>
在数组中存储值:
// ==========================================
$pcolor = array();
foreach ($_POST['product_color'] as $pc => $pcvalue) {
$pcolor[] .= $pcvalue;
}
$product_color = implode(', ', $pcolor);
// ==========================================
$psize = array();
foreach ($_POST['product_size'] as $ps => $psvalue) {
$psize[] .= $psvalue;
}
$product_size = implode(', ', $psize);
// ==========================================
$pid = array();
foreach ($_POST['product_id'] as $proid => $pidvalue) {
$pid[] .= $pidvalue;
}
$product_id = implode(', ', $pid);
// ==========================================
$pquantity = array();
foreach ($_POST['product_quantity'] as $pq => $pqvalue) {
$pquantity[] .= $pqvalue;
}
$product_quantity = implode(', ', $pquantity);
// ==========================================
$pname = array();
foreach ($_POST['product_name'] as $pn => $pnvalue) {
$pname[] .= $pnvalue;
}
$product_name = implode(', ', $pname);
// ==========================================
我的代码:
// Update Query
$update_quantity = "";
for ($i = 0; $i < (count($_SESSION['shopping_cart'])); $i++) {
$select_quan = "SELECT * FROM product WHERE pid = {$pid[$i]}";
$result_quan = mysqli_query($con, $select_quan);
while ($row_quan = mysqli_fetch_assoc($result_quan)) {
$quantity_after_order[$i] = $row_quan['pquantity'] - ($pquantity[$i]);
$update_quantity .= "UPDATE product SET pquantity = '$quantity_after_order[$i]' WHERE pid=$pid[$i]; ";
}
$res = mysqli_query($con,$update_quantity);
print_r($res);
}
print_r($update_quantity);
$update_quantity 查询运行完美但 mysqli_query 仅更新数据库中的第一个值和第二个产品值保持不变。
$res 的输出 = mysqli_query($con,$update_quantity)
1
update_quantity
的输出
UPDATE product SET pquantity = '211' WHERE pid=7; UPDATE product SET pquantity = '9' WHERE pid=31;
请帮助我如何更新数据库中的所有产品数量(用户订购)。
我找到了解决方案:
我正在使用
mysqli_query
我已将 mysqli_query 更新为
mysqli_multi_query
我的代码 运行 正确。
我的代码:
我从 while 循环中删除 mysqli_query:
$res = "";
$update_quantity = "";
for ($i = 0; $i < (count($_SESSION['shopping_cart'])); $i++) {
$select_quan = "SELECT * FROM product WHERE pid = {$pid[$i]}";
$result_quan = mysqli_query($con, $select_quan);
while ($row_quan = mysqli_fetch_assoc($result_quan)) {
$quantity_after_order[$i] = $row_quan['pquantity'] - ($pquantity[$i]);
$update_quantity .= "UPDATE product SET pquantity = '$quantity_after_order[$i]' WHERE pid=$pid[$i]; ";
}
}
并在我的项目中添加这部分代码:
// Execute multi query
if (mysqli_multi_query($con, $update_quantity)) {
do {
// Store first result set
if ($result = mysqli_store_result($con)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
// if there are more result-sets, the print a divider
if (mysqli_more_results($con)) {
}
//Prepare next result set
} while (mysqli_next_result($con));
}
这些更新后:
每个订单产品更新的产品数量。
谢谢
我正在从事库存商店项目。一切顺利,但我在订单成功后遇到了问题。 当用户下订单并重定向到结帐 Page.When 时,用户填写帐单并单击按钮。我 运行 在数据库中存储订单及其详细信息的过程以及在数据库中管理产品库存的过程。
来自隐藏字段的值:
<?php
foreach ($_SESSION['shopping_cart'] as $key => $value) {
?>
<input type="hidden" name="product_id[]" value="<?php echo $value['product_id'] ?>">
<input type="hidden" name="product_name[]" value="<?php echo $value['product_name'] ?>">
<input type="hidden" name="product_color[]" value="<?php echo $value['product_color'] ?>">
<input type="hidden" name="product_size[]" value="<?php echo $value['product_size'] ?>">
<input type="hidden" name="product_quantity[]" value="<?php echo $value['product_quantity'] ?>">
<?php } ?>
在数组中存储值:
// ==========================================
$pcolor = array();
foreach ($_POST['product_color'] as $pc => $pcvalue) {
$pcolor[] .= $pcvalue;
}
$product_color = implode(', ', $pcolor);
// ==========================================
$psize = array();
foreach ($_POST['product_size'] as $ps => $psvalue) {
$psize[] .= $psvalue;
}
$product_size = implode(', ', $psize);
// ==========================================
$pid = array();
foreach ($_POST['product_id'] as $proid => $pidvalue) {
$pid[] .= $pidvalue;
}
$product_id = implode(', ', $pid);
// ==========================================
$pquantity = array();
foreach ($_POST['product_quantity'] as $pq => $pqvalue) {
$pquantity[] .= $pqvalue;
}
$product_quantity = implode(', ', $pquantity);
// ==========================================
$pname = array();
foreach ($_POST['product_name'] as $pn => $pnvalue) {
$pname[] .= $pnvalue;
}
$product_name = implode(', ', $pname);
// ==========================================
我的代码:
// Update Query
$update_quantity = "";
for ($i = 0; $i < (count($_SESSION['shopping_cart'])); $i++) {
$select_quan = "SELECT * FROM product WHERE pid = {$pid[$i]}";
$result_quan = mysqli_query($con, $select_quan);
while ($row_quan = mysqli_fetch_assoc($result_quan)) {
$quantity_after_order[$i] = $row_quan['pquantity'] - ($pquantity[$i]);
$update_quantity .= "UPDATE product SET pquantity = '$quantity_after_order[$i]' WHERE pid=$pid[$i]; ";
}
$res = mysqli_query($con,$update_quantity);
print_r($res);
}
print_r($update_quantity);
$update_quantity 查询运行完美但 mysqli_query 仅更新数据库中的第一个值和第二个产品值保持不变。
$res 的输出 = mysqli_query($con,$update_quantity)
1
update_quantity
的输出 UPDATE product SET pquantity = '211' WHERE pid=7; UPDATE product SET pquantity = '9' WHERE pid=31;
请帮助我如何更新数据库中的所有产品数量(用户订购)。
我找到了解决方案:
我正在使用
mysqli_query
我已将 mysqli_query 更新为
mysqli_multi_query
我的代码 运行 正确。
我的代码:
我从 while 循环中删除 mysqli_query:
$res = "";
$update_quantity = "";
for ($i = 0; $i < (count($_SESSION['shopping_cart'])); $i++) {
$select_quan = "SELECT * FROM product WHERE pid = {$pid[$i]}";
$result_quan = mysqli_query($con, $select_quan);
while ($row_quan = mysqli_fetch_assoc($result_quan)) {
$quantity_after_order[$i] = $row_quan['pquantity'] - ($pquantity[$i]);
$update_quantity .= "UPDATE product SET pquantity = '$quantity_after_order[$i]' WHERE pid=$pid[$i]; ";
}
}
并在我的项目中添加这部分代码:
// Execute multi query
if (mysqli_multi_query($con, $update_quantity)) {
do {
// Store first result set
if ($result = mysqli_store_result($con)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
// if there are more result-sets, the print a divider
if (mysqli_more_results($con)) {
}
//Prepare next result set
} while (mysqli_next_result($con));
}
这些更新后:
每个订单产品更新的产品数量。
谢谢