简体   繁体   中英

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). Everything worked well until i realized that the writer stopped at the 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 ). Then you will end up have a foreach loop... the basic code will end up looking something like this:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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