2013-05-26 36 views
0

這是我的代碼,用於將我的數據插入到購物車中的數據庫中。當購物車中只有1個數據時,問題是否有效。如果購物車中有超過1件商品,我如何使它工作。我將它發送到數據庫的原因是,管理員可以查看訂單以準備它。將購物車數據插入到數據庫中

<?php 
require_once('auth.php'); 
?> 
<?php 
include("dbase.php"); 
include("functions.php"); 
if(isset($_REQUEST['command'])){ 
if($_REQUEST['command']=='update'){ 
$name=$_REQUEST['name']; 
$address=$_REQUEST['address']; 

$sum=get_order_total('grandTotal'); 

$result=mysql_query("insert into bloom_customers values('','$name','$address','$phone','$sum','$date','$time','$banks')"); 
$customer_id=mysql_insert_id(); 
$date=date('Y-m-d'); 

$result=mysql_query("insert into bloom_orders values('','$date','$customer_id')"); 
$orderid=mysql_insert_id(); 


$max=count($_SESSION['cart']); 
for($i=0;$i<$max;$i++){ 
    $proid=$_SESSION['cart'][$i]['product_id']; 
    $q=$_SESSION['cart'][$i]['qty']; 
    $price=get_price($proid); 
    mysql_query("insert into order_details values ($orderid,$proid,$q,$price,$customer_id)"); 
    $customer_id=mysql_insert_id(); 
} 
echo "<script type='text/javascript'>window.location='ordersubmitted.php' </script>"; 
} 
} 
?> 
+0

也許你不應該在的塊中的最後一行覆蓋CUSTOMER_ID? – fejese

+0

此外,您應該考慮成功後重定向header()調用,而不是使用js。 – fejese

+0

你是否在你的$ max變量中獲得所有項目? – bentham

回答

0

for循環的最後一行是:

$customer_id=mysql_insert_id(); 

這意味着,客戶價值正在改變每個插入。我想你的意思是:

$orderdetail_id=mysql_insert_id(); 

而且,你不需要保存客戶ID order_details表。它已經在orders表中。

最後,所有的刀片應該具有明確的列清單,如:

$result=mysql_query("insert into bloom_orders(date, customer_id) values('$date', '$customer_id')"); 
相關問題