从多维数组向mysql插入数据时出现轻微错误

时间:2013-07-30 00:28:25

标签: php database multidimensional-array insert mysqli

<?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 egg,$ 1.00 2QTY item2鸡,$ 30,1QTY

点击提交后,在我的sql表中只显示item2 chicken但是$ 1.00和2QTY应该属于item 1 egg。我应该怎么解决这个问题?我尝试将$ 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())

如果只选择了一个项目并提交

,则弹出这个

1 个答案:

答案 0 :(得分: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();

}     }

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