如果之前已经提出这个问题,我想提前道歉。我一直在浏览这个网站几个小时试图找到我正在寻找的答案,但没有运气。
这是我的问题:
我根据Larry Ullman(PHP和MySQL for Dynamic Websites Edition 1)的书中的教程创建了这个在线购物车。一切顺利,直到我意识到作家停在checkout.php
我需要帮助编制结帐页面。我最大的问题是将购物车中的多个产品作为单独的行插入数据库。 有人可以帮忙吗?
感谢。
这是我到目前为止所拥有的:
<?php
session_start();
if (is_numeric ($_GET['pid'])) {
$pid = $_GET['pid'];
if (isset ($_SESSION['cart'][$pid])) {
$qty = $_SESSION['cart'][$pid] + 1;
} else {
$qty = 1;
}
$_SESSION['cart'][$pid] = $qty;
echo '<p>The item has been added to your shopping cart.</p>';
}
if (isset ($_POST['submit'])) {
foreach ($_POST['qty'] as $key => $value) {
if ( ($value == 0) AND (is_numeric ($value)) ) {
unset ($_SESSION['cart'][$key]);
} elseif ( is_numeric ($value) AND ($value > 0) ) {
$_SESSION['cart'][$key] = $value;
}
}
}
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
}
}
}
if (!$empty) {
include("config.php");
$query = 'SELECT * FROM buds_customer, buds_product WHERE buds_customer.customer_id = buds_product.customer_id AND buds_product.print_id IN (';
foreach ($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY buds_customer.last ASC';
$result = mysql_query ($query);
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="30%"><b>Seller</b></td>
<td align="left" width="30%"><b>Product</b></td>
<td align="right" width="10%"><b>Price</b></td>
<td align="center" width="10%"><b>Qty</b></td>
<td align="right" width="10%"><b>Total Price</b></td>
</tr>
<form action="view_cart.php" method="post">
';
$total = 0; // Total cost of the order.
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$subtotal = $_SESSION['cart'][$row['print_id']] * $row['price'];
$total += $subtotal;
echo " <tr>
<td align=\"left\"><input type=\"text\" name=\"seller\" value=\" {$row['first']} {$row['last']}\"></td>
<td align=\"left\"><input type=\"text\" name=\"product\" value=\" {$row['product']}\"></td>
<td align=\"right\"><input type=\"text\" name=\"price\" value=\" {$row['price']}\"></td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['print_id']}]\" value=\"{$_SESSION['cart'][$row['print_id']]}\" /></td>
<td align=\"right\"><input type=\"text\" name=\"subtotal\" value=\"" . number_format ($subtotal, 2) . "\"></td>
</tr>\n";
}
echo ' <tr>
<td colspan="4" align="right"><b>Total:<b></td>
<td align="right"><input type="text" size="3" name="total" value="' . number_format ($total, 2) . '"></td>
</tr>
</table><div align="center"><input type="submit" name="submit" value="Update My Cart" /></form><br /><br /><center><a href="checkout.php">Checkout</a></center></div>
';
} else {
echo mysql_error();
}
?>
答案 0 :(得分:1)
您的示例根本没有显示任何插入语句...您应该查找并学习INSERT INTO(http://www.w3schools.com/php/php_mysql_insert.asp)。然后你最终会有一个foreach循环...基本代码最终会看起来像这样:
foreach ($items as $item) {
$sql = 'INSERT INTO `order_history` (`productid`, `productqty`)'
. ' VALUES ($item['product_id'], $item['product_qty']);
mysql_query($sql);
}
当然我要省略错误检查以及你想要填充的各种额外字段......但是你明白了。祝你好运!