Minor从多维数组向mysql插入数据时出错 [英] Minor Error when insert data to mysql from multidimensional array

查看:159
本文介绍了Minor从多维数组向mysql插入数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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数据库和下面的尝试插入数据到我创建的命名为订单的新数据库

This the above is the multidimensional array that grab the variable from mySQL database and the below try to insert the data into the new database i created named "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的信用),但它错过了顶部项目,例如在我的购物车中显示:

the above code success to insert data into orders table (credit to Perry) but, it miss up the top item eg in my cart show:

item1 egg,$ 1.00 2QTY
item2 chicken,$ 30,1QTY

item1 egg, $1.00 2QTY item2 chicken,$30,1QTY

我点击提交后,在我的sql表显示只有item2鸡,但$ 1.00和2QTY,应属于项目1鸡蛋。我应该如何解决这个问题?我试图更改$ i到'-1','1'都仍然不工作。感谢

after i clicked submit, in my sql table showns only item2 chicken but with $1.00 and 2QTY which should belong to item 1 egg. how should i fix this? i tried changing the $i to '-1','1' both still not working. Thanks

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())

这是什么弹出如果只有一个项目选择并提交

this is what pop out if only one item selected and submit

推荐答案

如果你说这是一个多维数组,您的代码如下

if you says it is a multidimensional array you should write your code like this

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

}
}

现在这将显示一个拖曳维数阵列
为三维数组使用第三个变量

now this will display an tow dimensional array for three dimensional array use a third variable

这篇关于Minor从多维数组向mysql插入数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆