2013-07-30 69 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

你確定這個數組是多維的並且有多少維數? –