问题
I can't find where is the problem at my code. It only send single orders to the database. When I order in my cart 2 or more than items, it only sends the last order. I don't have any idea how I can change or add some syntax in my code.
Here is my code in checkout.php
<?php
session_start();
echo '<h3>Your Order</h3>';
$current_url = base64_encode($url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION['products'])){
echo '<ol>';
echo '<form action="checkout_with_us.php" method="POST">';
$total = 0;
$cart_items = 0;
foreach($_SESSION['products'] as $cart_itm){
$product_code = $cart_itm['code'];
$results = $mysqli->query("SELECT product_name,product_desc,price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
echo '<li>';
echo 'Price: '.$currency.$obj->price;
echo '<h4>'.$obj->product_name.'(Code: '.$product_code.')</h4>';
echo 'Qty: '.$cart_itm['qty'];
echo '</li>';
$subtotal = ($cart_itm['price'] * $cart_itm['qty']);
$total = ($total + $subtotal);
$cart_items++;
echo '<input type="hidden" name="item_name" value="'.$obj->product_name.'">';
echo '<input type="hidden" name="item_desc" value="'.$obj->product_desc.'">';
echo '<input type="hidden" name="item_qty" value="'.$cart_itm["qty"].'">';
echo '<input type="hidden" name="item_code" value="'.$product_code.'">';
}
echo '<strong>Sub Total: '.$currency.$total.'</strong>';
echo '<input type="hidden" name="price" value="'.$total.'">';
echo '</ol>';
}
//Here is the information of the customer
echo 'Firstname: <input type="text" name="firstname"><br />';
echo 'Lastname: <input type="text" name="lastname"><br />';
echo 'Email: <input type="text" name="email"><br />';
echo '<input type="submit" value="Send Step">';
echo '</form>';
?>
And here is my checkout.with_us.php codes. This code is the bridge to send the information to the database.
<?php
session_start();
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$order_name = $_POST['item_name'];
$order_code = $_POST['item_code'];
$order_qty = $_POST['item_qty'];
$sub_total = $_POST['price'];
$conn = mysqli_connect('localhost','root','','sampsix')or die('Could not connect');
$query = "INSERT INTO `sampsix`.`orders`(`firstname`,`lastname`,`email`,`OrderName`,`OrderCode`,`OrderQty`,`SubTotal`) VALUES('$firstname','$lastname','$email','$order_name','$order_code','$order_qty','$sub_total')";
mysqli_query($conn,$query);
mysqli_close($conn);
header('Location: checkout.php');
?>
回答1:
Delete your other question, ok?
The problem is you loop through $_SESSION
and use the same name
value each time. You need to create an array of your inputs. Here is an example:
<?php
echo '<h3>Your Order</h3>';
$current_url = base64_encode($url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION['products'])){
echo '<ol>';
echo '<form action="checkout_with_us.php" method="POST">';
$total = 0;
$cart_items = 0;
foreach($_SESSION['products'] as $cart_itm){
$product_code = $cart_itm['code'];
$results = $mysqli->query("SELECT product_name,product_desc,price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
echo '<li>';
echo 'Price: '.$currency.$obj->price;
echo '<h4>'.$obj->product_name.'(Code: '.$product_code.')</h4>';
echo 'Qty: '.$cart_itm['qty'];
echo '</li>';
$subtotal = ($cart_itm['price'] * $cart_itm['qty']);
$total = ($total + $subtotal);
$cart_items++;
echo '<input type="hidden" name="product['.$product_code.'][item_name]" value="'.$obj->product_name.'">';
echo '<input type="hidden" name="product['.$product_code.'][item_desc]" value="'.$obj->product_desc.'">';
echo '<input type="hidden" name="product['.$product_code.'][item_qty]" value="'.$cart_itm["qty"].'">';
echo '<input type="hidden" name="product['.$product_code.'][item_code]" value="'.$product_code.'">';
}
echo '<strong>Sub Total: '.$currency.$total.'</strong>';
echo '<input type="hidden" name="product['.$product_code.'][price]" value="'.$total.'">';
echo '</ol>';
}
//Here is the information of the customer
echo 'Firstname: <input type="text" name="firstname"><br />';
echo 'Lastname: <input type="text" name="lastname"><br />';
echo 'Email: <input type="text" name="email"><br />';
echo '<input type="submit" value="Send Step">';
echo '</form>';
?>
You can catch this by looping in your product
array:
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$conn = mysqli_connect('localhost','root','','sampsix')or die('Could not connect');
foreach($_POST['product'] as $product)
{
$order_name = $product['item_name'];
$order_code = $product['item_code'];
$order_qty = $product['item_qty'];
$sub_total = $product['price'];
$query = "INSERT INTO `sampsix`.`orders`(`firstname`,`lastname`,`email`,`OrderName`,`OrderCode`,`OrderQty`,`SubTotal`) VALUES('$firstname','$lastname','$email','$order_name','$order_code','$order_qty','$sub_total')";
mysqli_query($conn,$query);
}
mysqli_close($conn);
header('Location: checkout.php');
?>
I don't know what the purpose is of the table orders
but with my example the products will be added to this table with the same firstname
, lastname
, etc.
来源:https://stackoverflow.com/questions/25825183/mysqli-and-php-sends-only-single-products-on-the-database