2013-07-30 71 views
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数据库抢变量和下面的尝试将数据插入到新的数据库,我创建了一个名为“订单”次要错误时插入数据到MySQL从多维数组

多维数组
<?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(); 
} 
?> 

上面的代码的成功将数据插入到订单表(信贷佩里),但它错过了,例如在我的购物车显示顶部的项目:

物品1鸡蛋,$ 1.00包装2QTY ITEM2鸡,$ 30,1QTY

我点击提交后,在我的sql表中showns只有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()) 

这是蹦出来,如果只选择一个项目,并提交

+0

你能显示你的POST吗?使用此代码:'echo'

'; print_r($_POST); echo '
';'并在此处发布:)尝试使用 – Perry

回答

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

} }

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

+0

,但它表示在/home/u382560552/public_html/order_list.php上解析错误:语法错误,意外的','期待']' 27 –

+0

我修正了这个错误,但最终却出现致命错误 –

+0

你确定这个数组是多维的并且有多少维数? –