How to insert ordering data from multidimensional array

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

<?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>'; }  ?>   

The above is the source code I learnt online to create a multidimensional array and submit to paypal for processing. Now I try to avoid paypal and wanna to insert the data submitted to my new database 'orders'. I created row (id (primary,auto increment),product_name,price,quantity,date_added) for this purpose but I failed to insert the data into it....

my attempt as below

HELP~~~ I need to know what shall I put in if (isset($_POST['cartOutput'])) { in order to retrieve the date...i am really a beginner. I need some magic......

回答1:

My example use PDO but I think you get the idea, Also you should move to PDO or MYSQLI instead of mysql

the example:

<?php // pdo example  $sql = 'INSERT INTO table (field1, field2, field3) VALUES (:value1, :value2, :value3)';  // $dbh is pdo connection $insertTable = $dbh->prepare($sql);  $countArray = count($array);  for ($i = 0; $i < $countArray; $i++) {    $insertTable->bindParam(':value1', $array['value1'][$i], PDO::PARAM_INT); // if value is int    $insertTable->bindParam(':value2', $array['value2'][$i], PDO::PARAM_STR); // if value is str    $insertTable->bindParam(':value3', $array['value3'][$i], PDO::PARAM_STR);    $insertTable->execute(); }  ?> 

Input name format

I see you are doing this: amount_' . $x . ' your array post will look like this:

[amount_0] => 100 [amount_1] => 200 [amount_2] => 1 [quantity] => 10 [quantity] => 20 [quantity] => 1 

but if your write amount[] the array will look like this:

[amount] => Array     (         [0] => 100         [1] => 200         [2] => 1     )  [quantity] => Array     (         [0] => 10         [1] => 20         [2] => 1 

The last option makes it much better to read the array.

MYSQLI example

<?php $sql = 'INSERT INTO table (field1, field2, field3) VALUES (?, ?, ?)'; $stmt = $mysqli->prepare($sql);   $countArray = count($array);  for ($i = 0; $i < $countArray; $i++) { $stmt->bind_param('ssd', $array['value1'][$i], $array['value2'][$i], $array['value3'][$i]);    $stmt->execute(); }  ?> 

As you can see there is standing ssd before the parameters these are the types there are 4 types:

  • i = intenger
  • s = string
  • d = double
  • b = blob

You must always define this.

Edit

You should use this:

<?php  // Parse the form data and add inventory item to the system if (isset($_POST['cartOutput'])) {   $sql= 'INSERT INTO orders (product_name, price, quantity, 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['amount'][$i], $_POST['quantity'][$i]); $stmt->execute(); }  echo $sql   ;   exit(); } ?> 


回答2:

1) Your form action for pp_checkout_btn still shows "paypal web". I think that's just a typo, so please go ahead and change that first.

2) Moreover, your insert statement can use a little tweaks. Please provide results for the following two statements, for framing an exact insert statement:

print_r($_POST) // once the data has been posted echo $sql // place this just before calling exit 

-- seekers01



回答3:

I think your insert code should be as below
$sql = mysql_query("INSERT INTO orders (product_name, price, quantity, date_added) VALUES('" . $_POST[$product_name] . "','" . $_POST[$price] . "','" . $_POST[$each_item['quantity']] . "', now())") or die (mysql_error());

Edit:
Also, the statement inside for loop should be
$stmt->bind_param('ssd', $array[$product_name][$i], $array[$price][$i], $array[$each_item['quantity']][$i]);



回答4:

Here is the code you should refer to:

To retrieve the date:

$date = date('d/m/Y', time()); 

Try this and then see if it works.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!