我在购物车中有购物车中显示的商品。例如:我有3个不同的数量项目 我设法将它回显到购物车页面,并将订单写入ORDERS表。
<?php
...
$sql = "SELECT id_item, item, price FROM id_item WHERE id_item IN (";
foreach ($_SESSION['cart'] as $id => $value) {
$sql .=$id.",";
}
$sql = substr($sql, 0, -1).") ORDER BY item ASC";
$query =mysql_query($sql);
$total = 0;
while($row=mysql_fetch_array($query)) {
$subtotal = $_SESSION['cart'][$row['id_item']]['quantity']*$row['price'];
$total += $subtotal;
?>
<tr>
<td width="200px" style="padding-left:20px;"><?= $row['item'] ?></td>
<td style="padding-left:20px;"><input type="text" name="quantity[<?= $row['id_item']?>]" size="5" value="<?= $_SESSION['cart'][$row['id_item']]['quantity']?>"</td>
<td style="padding-left:20px;"><?= $row['price']?></td>
<td style="padding-left:20px;"><?= $_SESSION['cart'][$row['id_item']]['quantity']*$row['price']?> kn</td>
</tr>
...
所以现在我想在ORD.ITEMS表中写一个订单,因为它有多个项目,所以我被困在这里。
表ORDERS和ORD.ITEMS在id_order上有关系,表ORD.ITEMS和ITEMS依赖于item&amp; id_item
ORDERS
id_order
user
date
status -> this I'll try on my own, if status is 0 order is open, if 1 order closed, if 2 order
ORD.ITEMS
id_order
id_ord.items
item
quantity
ITEMS
id_item
title
description
price
所以现在我想要三个不同的项目:
ORDERS
1
user1
2014-01-01
0
ORD.ITEMS
1
1
10
5
ORD.ITEMS
1
2
20
3
ORD.ITEMS
1
3
30
2
如何编写SQL查询,在完成订单后写入表ORD.ITEMS三项?
答案 0 :(得分:1)
我设法解决了我的问题,很容易。这是我解决它的一种方式:
$sqlMaxS = "SELECT MAX(id_ord_items) FROM ord.items";
$resultMaxS = queryDB($sqlMaxS);
$result = mysql_fetch_row($resultMaxS);
$id_ord_items = $result['0'];
$id_ord_items++;
$sql = "SELECT * FROM items WHERE id_item IN (";
foreach ($_SESSION['cart'] as $id => $value) {
$sql .=$id.",";
}
$sql = substr($sql, 0, -1).") ORDER BY name ASC";
$query =mysql_query($sql);
while($row=mysql_fetch_array($query)) {
$sql = "INSERT INTO ord.items (id_order, id_ord_items, item, quantity)
VALUES ('$id_order', '$id_ord_items', {$row['id_item']}, {$_SESSION['cart'][$row['id_item']]['quantity']})";
$stavka++;
queryDB($sql);
就像上面我使用while循环一样。