我的购物车页面上有两个链接。一个是“继续购物”,一个是产品目录,另一个是“结账”,它通往订单详情页面。问题是,当我刷新订单详细信息页面时,我的order_id会增加,并且新订单会存储在我不想要的表中。我使用header(..)来阻止它,但它似乎也没有用。
session_start();
if(!isset($_SESSION["customer"]))
{
header("location: customer_login.php"); // will not execute if loggedin.
exit();
}
// Connect to the MYSQL database.
include "storescripts/connect_to_mysql.php";
// Checks to see that the URL variable is set and that it exists in the database.
// Will be used to output the order elements.
$cartOutput = "";
if(isset($_SESSION["cart_array"]) && !empty($_SESSION["cart_array"]))
{
$customer=$_SESSION["customer"];
// Get the customer id against the username of the customer.
$sql=mysql_query("select customer_id from customer c where user_name='$customer'");
$productCount = mysql_num_rows($sql); // count the output amount.
if($productCount > 0)
{
while($row = mysql_fetch_array($sql))
{
$customer_id=$row["customer_id"];
}
}
else
{
echo "This user doesn't exist in our database.";
exit();
}
mysql_query("insert into orders(order_date,customer_id,payment_id)
values(now(),'$customer_id',2)")or die(mysql_error());
// Get the order id of the customer in order to store information in order2product table.
$sql2=mysql_query("select order_id from orders where customer_id='$customer_id'") or
die(mysql_error());
$productCount = mysql_num_rows($sql2); // count the output amount.
if($productCount > 0)
{
while($row = mysql_fetch_array($sql2))
{
$order_id=$row["order_id"];
}
}
else
{
echo "The customer id which you specified doesn't have an order id against it.";
exit();
}
foreach($_SESSION["cart_array"] as $listitem)
{
$item_id=$listitem['item_id'];
$quantity=$listitem['quantity'];
mysql_query("insert into order2product(Product_id,Order_id,quantity)
values('$item_id','$order_id','$quantity')") or die(mysql_error());
}
header ("location:order_page.php");
exit();
} most outer if condition ends
else ...
答案 0 :(得分:0)
开发购物车并使用mysql_*
功能?你问了很多问题。我建议PDO。
无论如何,为什么不将order_id
绑定到会话并将其绑定到数据库..检查会话是否存在。如果存在,请检查数据库。如果它也存在,请使用它!如果没有,那么您可以生成新的ID。从那里,当您看到空订单时 - 您可以假设它们在会话超时期后被放弃。当然,还有其他优雅的解决方案,但这应该让你开始。