简体   繁体   English

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

[英]PHP - How can I insert multiple items from a shopping cart into the database?

I'd like to apologize in advance if this question has been asked before. 如果您曾经问​​过这个问题,我想提前道歉。 I've been surfing this website for a couple of hours trying to find the answer I'm looking for but no luck. 我已经在这个网站上冲浪了几个小时,试图找到我想要的答案,但是没有运气。

Here's my problem: 这是我的问题:

I've created this online shopping cart based on a tutorial from a book by Larry Ullman (PHP and MySQL for Dynamic Websites Edition 1). 我已经根据Larry Ullman的书(动态网站版的PHP和MySQL)的一本书的教程创建了此在线购物车。 Everything worked well until i realized that the writer stopped at the checkout.php 一切正常,直到我意识到作者停在checkout.php为止

I need help coding the checkout page. 我需要对结帐页面进行编码的帮助。 My biggest problem is inserting multiple products from the shopping cart into the database as individual rows. 我最大的问题是将购物车中的多个产品作为单独的行插入数据库中。 Can anyone help? 有人可以帮忙吗?

Thanks. 谢谢。

Here's what i have so far: 这是我到目前为止的内容:

<?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();
}


?>

Your example doesn't show any insert statements at all... You should lookup and learn INSERT INTO ( http://www.w3schools.com/php/php_mysql_insert.asp ). 您的示例根本不显示任何插入语句...您应该查找并学习INSERT INTO( http://www.w3schools.com/php/php_mysql_insert.asp )。 Then you will end up have a foreach loop... the basic code will end up looking something like this: 然后,您将最终得到一个foreach循环...基本代码将最终看起来像这样:

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

Of course I'm leaving out error checking and all kinds of extra fields you will want to populate... but you get the idea. 当然,我没有进行错误检查以及您想要填充的各种其他字段……但是您知道了。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM