1
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5  (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
    // Start PayPal Checkout Button
    $pp_checkout_btn .= '<form action="http://chenlikpharmacy.freeserver.me/order_list.php" method="post">
    <input type="hidden" name="cartOutput" id="cartOutput" value = "<?php echo $cartOutput; ?>
    ';
    // Start the For Each loop
    $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
$sqlCommand = "SELECT * FROM products WHERE id='$item_id' LIMIT 1";     
$sql = mysqli_query($myConnection,$sqlCommand);
        while ($row = mysqli_fetch_array($sql)) {
            $product_name = $row["product_name"];
            $price = $row["price"];
            $details = $row["details"];
        }
        $pricetotal = $price * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
        setlocale(LC_MONETARY, "ms_MY");
        $pricetotal = money_format("%10.2n", $pricetotal);
        // Dynamic Checkout Btn Assembly
        $x = $i + 1;
        $pp_checkout_btn .= '<input type="hidden" name="item_name[]" value="' . $product_name . '">
        <input type="hidden" name="amount[]" value="' . $price . '">
        <input type="hidden" name="quantity[]" value="' . $each_item['quantity'] . '">  ';
        // Create the product array variable
        $product_id_array .= "$item_id-".$each_item['quantity'].","; 
        // Dynamic table row assembly
        $cartOutput .= "<tr>";
        $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
        $cartOutput .= '<td>' . $details . '</td>';
        $cartOutput .= '<td>RM ' . $price . '</td>';
        $cartOutput .= '<td><form action="cart.php" method="post">
        <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
        <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
        <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
        </form></td>';
        //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
        $cartOutput .= '<td>' . $pricetotal . '</td>';
        $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
        $cartOutput .= '</tr>';
        $i++; 
    } 
    setlocale(LC_MONETARY, "ms_MY");
    $cartTotal = money_format("%10.2n", $cartTotal);
    $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." MYR</div>";
    // Finish the Paypal Checkout Btn
    $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">

    <input type="submit" type="button" name="submit">
    </form>';
}

?> 

上面是从 mySQL 数据库中获取变量的多维数组,下面尝试将数据插入到我创建的名为“orders”的新数据库中

<?php 
// This file is www.developphp.com curriculum material
// Written by Adam Khoury January 01, 2011
// http://www.youtube.com/view_play_list?p=442E340A42191003
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database  
include "storescripts/connect_to_mysqli.php"; 
?>


<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['cartOutput'])) { 

$sql= 'INSERT INTO orders (product_name,  quantity,price, date_added) VALUES(?,?,?, NOW())';      

$stmt = $myConnection->prepare($sql); 

$countArray = count($_POST['item_name']);

for ($i = 0; $i < $countArray; $i++) {
$stmt->bind_param('sss', $_POST['item_name'][$i], $_POST['quantity'][$i], $_POST['amount'][$i]);
$stmt->execute();
}

echo $sql   ; 

exit();
}
?>

上面的代码成功地将数据插入到订单表中(归功于 Perry),但是它错过了顶部的项目,例如在我的购物车显示中:

item1 鸡蛋, $1.00 2QTY item2 鸡肉,$30,1QTY

在我点击提交后,在我的 sql 表中只显示了 item2 鸡,但有 $1.00 和 2QTY 应该属于项目 1 鸡蛋。我应该如何解决这个问题?我尝试将 $i 更改为 '-1','1' 仍然无法正常工作。谢谢

Notice: Undefined index: item_name in /home/u382560552/public_html/order_list.php on line 22
INSERT INTO orders (product_name, quantity,price, date_added) VALUES(?,?,?, NOW())

如果只选择一项并提交,这就是弹出的内容

4

1 回答 1

0

如果你说它是一个多维数组,你应该这样写你的代码

for ($i = 0; $i < $countArray; $i++) {
for ($j=0; &j < $countArray ; $j++){
$stmt->bind_param('sss', $_POST['item_name'][$i,$j], $_POST['quantity'][$i,$j], $_POST['amount'][$i,&j]);
$stmt->execute();

} }

现在这将显示一个二维数组,用于三维数组使用第三个变量

于 2013-07-30T00:47:35.157 回答