PHP-如何将购物车中的多个项目插入数据库?

PHP-如何将购物车中的多个项目插入数据库?,php,html,Php,Html,如果以前有人问过这个问题,我想提前道歉。我已经在这个网站上浏览了几个小时,试图找到我正在寻找的答案,但是运气不好 我的问题是: 我根据Larry Ullman(PHP和MySQL for Dynamic Websites Edition 1)的一本书中的教程创建了这个在线购物车。一切都很顺利,直到我意识到作者在checkout.php处停了下来 我需要帮助编码结帐页面。我最大的问题是将购物车中的多个产品作为单独的行插入数据库。 有人能帮忙吗 谢谢 以下是我目前掌握的情况: <?php

如果以前有人问过这个问题,我想提前道歉。我已经在这个网站上浏览了几个小时,试图找到我正在寻找的答案,但是运气不好

我的问题是:

我根据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();
}


?>

您的示例根本没有显示任何insert语句。。。您应该查找并学习插入()。然后你会有一个foreach循环。。。基本代码最终将显示如下所示:

foreach ($items as $item) {
    $sql = 'INSERT INTO `order_history` (`productid`, `productqty`)'
        . ' VALUES ($item['product_id'], $item['product_qty']);
    mysql_query($sql);
}

当然,我忽略了错误检查和所有你想填充的额外字段。。。但你明白了。祝你好运

你得缩小你的问题范围。大多数人不会为您编写代码。如果你记录你的资料来源,这样人们就会知道你想对每个部分做什么。谢谢你的快速回复。这是我最初使用的代码,这段代码将产品输入数据库,但除了数量字段(qty)之外,每一行都是相同的。