When I click on checkout, it will send me an email with my order and send the products I ordered to mysql database. But only one(the last product) product is send and saved in the database. How can I fix this?
//connect to database
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");
//get order id ONLY to get order id <<<<<<<
$vol = mysql_query("SELECT orderid FROM ordertracking WHERE email='$email'");
while($volume=mysql_fetch_array($vol))
{
$orderid = $volume['orderid'];
} // end of getting order id from table ordertracking
// add new order
$order = "INSERT INTO `order` (orderid, customerid, productid, brand, model, price, amount, totalcost, image) VALUES ('$orderid', '$customerid', '$productid', '$brand' , '$model', '$price', '$amount', '$totalcost', '$image')";
if (!mysql_query($order,$connection))
{
die('Error: ' . mysql_error());
echo "Sorry, there was an error";
}
echo "New order added" . "<br />";
mysql_close($connection);
Entire script:
<?php
error_reporting(-1);ini_set('display_errors', 'stdout');
var_dump($_GET);
//collect all information
$name = $_GET["name"];
$surname = $_GET["surname"];
$city = $_GET["city"];
$postalcode = $_GET["postalcode"];
$phonenumber = $_GET["phonenumber"];
$email = $_GET["email"];
$i = 1;
while (isset($_GET["Product_ID_".$i])) {
$productid = $_GET["Product_ID_".$i];
$brand = $_GET["Brand_".$i];
$model = $_GET["Model_".$i];
$price = $_GET["Price_".$i];
$amount = $_GET["Amount_products_".$i];
$totalcost = $_GET["Total_cost_".$i];
$i++;
}
$image = "includes/images/mouse_4.jpg";
$date = date("F j, Y, g:i a");
//connect to database
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");
//check if already customer
$result = mysql_query("SELECT * FROM customer WHERE email='$email'");
$rows = mysql_num_rows($result);
if ($rows)
{
echo '<br>Welcome back ' . $name .' '. $surname. '<br>';
}
else
{
//if new customer, add to database
$customer = "INSERT INTO customer (customerid, name, surname, email, city, postalcode, phonenumber) VALUES ('', '$name', '$surname', '$email', '$city', '$postalcode', '$phonenumber')";
if (!mysql_query($customer,$connection))
{
die('Error: ' . mysql_error());
echo "Sorry, there was an error";
}
echo "New customer added" . "<br />";
echo '<br>Welcome as our new customer ' . $name . ' '. $surname;
mysql_close($connection);
}
//connect to database
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");
//get customer id
$res = mysql_query("SELECT customerid FROM customer WHERE email='$email'");
while($row=mysql_fetch_array($res))
{
$customerid=$row['customerid'];
}
//add new ordertracking
$ordertracking = "INSERT INTO `ordertracking` (orderid, customerid, email, progress, date) VALUES ('', '$customerid', '$email', 'Pending', '$date')";
if (!mysql_query($ordertracking,$connection))
{
die('Error: ' . mysql_error());
echo "Sorry, there was an error";
}
echo "New order added" . "<br />";
mysql_close($connection);
//connect to database
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");
//get order id
$vol = mysql_query("SELECT orderid FROM ordertracking WHERE email='$email'");
while($volume=mysql_fetch_array($vol))
{
$orderid = $volume['orderid'];
}
// add new order
$order = "INSERT INTO `order` (orderid, customerid, productid, brand, model, price, amount, totalcost, image) VALUES ('$orderid', '$customerid', '$productid', '$brand' , '$model', '$price', '$amount', '$totalcost', '$image')";
if (!mysql_query($order,$connection))
{
die('Error: ' . mysql_error());
echo "Sorry, there was an error";
}
echo "New order added" . "<br />";
mysql_close($connection);
$to = $email;
$subject = "Order information of: ";
$headers = "From: " . "postmaster@localhost" . "\r\n";
$headers .= "Reply-To: ". "postmaster@localhost" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>Dear ' . $name . ' ' . $surname . ',</h1>' . '<br />';
$message .= 'Order date and time: ' . $date . '<br />';
$message .= 'Thank you for your order at our online shop!' . '<br />';
$message .= 'Your order information: ' . '<br /><br /><br />';
$i = 1;
while (isset($_GET["Product_ID_".$i])) {
$productid = $_GET["Product_ID_".$i];
$brand = $_GET["Brand_".$i];
$model = $_GET["Model_".$i];
$price = $_GET["Price_".$i];
$amount = $_GET["Amount_products_".$i];
$totalcost = $_GET["Total_cost_".$i];
$message .= ' Product ID: ' . $productid . "<br />" .
'Brand: '. $brand . "<br />" .
'Model: ' . $model . "<br />" .
'Price per item: ' . $price . "<br />" .
'Amount of item: ' . $amount . "<br />" .
'Total cost: ' . $totalcost . "<br />" .
'_________________________________________________| ' . "<br />" .
$i++;
}
$message .= 'To follow your odertracking please remember your order ID and customer ID' . '<br />';
$message .= 'Order ID: ' . $orderid . '<br />';
$message .= 'Customer ID: ' . $customerid . '<br />';
$message .= 'Link to track your order: ' . '<a href="http://localhost/school/shoppingcart/ordertracking.php">Ordertracking system</a>' . '<br />';
$message .= '</body></html>';
mail($to, $subject, $message, $headers);
?>
what you are doing here is this:
the code
works great but when you try to insert the value of orderid to [order] table only one row is updated because the variable $orderid now contains only one string (The last row’s orderid of the query).
so the solution is to run insert inside the while loop
something like this
hope you will get an idea.
the second solution can be that instead of using simple variable $orderid you can use an array variable. then loop through the variable to get the orderid’s.
in short according to your code you need a loop to insert more than one record in the database.