2011-11-08 51 views
0

我有一個購物車,在此時將客戶帶到數據庫的項目發送給我,但現在我已包含一個登錄系統,您必須成爲成員在您購買物品之前。我已將登錄的用戶保存在一個會話中,因此我一直在嘗試將會話變量發送到數據庫。目前,我有3個表內的客戶,訂單和order_detail(參見下面的代碼):將購物車詳細信息插入MySQL數據庫使用PHP

session_start(); 
?> 
<?php 
if(!isset($_SESSION["username"])) 
{ 
    header("Location: shoppinglogin.php"); 
} 
?> 

<? 
    include("includes/db.php"); 
    include("includes/functions.php"); 

    if($_REQUEST['command']=='update'){ 
     $name=$_REQUEST['name']; 
     $email=$_REQUEST['email']; 
     $address=$_REQUEST['address']; 
     $phone=$_REQUEST['phone']; 

     $result=mysql_query("insert into customers values('','$name','$email','$address','$phone')"); 
     $customerid=mysql_insert_id(); 
     $date=date('Y-m-d'); 
     $result=mysql_query("insert into order values('','$date','$customerid')"); 
     $orderid=mysql_insert_id(); 

     $max=count($_SESSION['cart']); 
     for($i=0;$i<$max;$i++){ 
      $pid=$_SESSION['cart'][$i]['productid']; 
      $q=$_SESSION['cart'][$i]['qty']; 
      $price=get_price($pid); 
      mysql_query("insert into order_detail values ($orderid,$pid,$q,$price)"); 
     } 
     die('Thank You! your order has been placed!'); 
     session_unset(); 
    } 
?> 

我已經改變成下面的代碼:

<?php 

session_start(); 
?> 
<?php 
if(!isset($_SESSION["username"])) 
{ 
    header("Location: shoppinglogin.php"); 
} 
?> 

<? 
    include("includes/db.php"); 
    include("includes/functions.php"); 

    if($_REQUEST['command']=='update'){ 
     $name=$_REQUEST['name']; 
     $email=$_REQUEST['email']; 
     $address=$_REQUEST['address']; 
     $phone=$_REQUEST['phone']; 

$max=count($_SESSION['cart']); 
     for($i=0;$i<$max;$i++){ 
      $orderid=mysql_insert_id(); 
      $pid=$_SESSION['cart'][$i]['productid']; 
      $q=$_SESSION['cart'][$i]['qty']; 
      $price=get_price($pid); 
      $date=date('Y-m-d'); 
      $user=$_SESSION['username']; 
      mysql_query("insert into order values ($orderid,$pid,$q,$price,$date,$user)"); 
     } 
     die('Thank You! your order has been placed!'); 
     session_unset(); 
    } 
?> 

上面的代碼不將任何東西插入我的訂單表中。

感謝

回答

0

呃。完全沒有錯誤處理的數據庫操作。假設數據庫查詢成功,只會讓你陷入這種情況 - 不知道什麼是錯的。

在完全裸露的最小值,你的數據庫操作應該是這樣的:

$sql = "... query goes here ..." 
$result = mysql_query($sql); 
if ($result === FALSE) { 
    die("Query failed!" . mysql_error() . $sql); 
} 

至少停止腳本死在其軌道上,告訴您該查詢失敗,告訴你爲什麼會失敗,並告訴你是什​​麼查詢。

以及您的代碼是寬敞開放SQL injection攻擊。這顯然是一個電子商務設置,這尤其糟糕。我建議你立即關閉這個系統,直到你有機會讀到這個並堵塞漏洞。

0

只是請求mysql_query功能後嘗試or die(mysql_error())。這可能會給你更多關於這個問題的信息......

0

請確保如果您的查詢'封閉每個值,

嘗試用這種替代:

insert into order values ('$orderid','$pid','$q','$price','$date','$user')

,並確保該表order沒有其他領域不屬於當不指定時爲空:

insert into order (order_id, product_id, qty, price, order_date, order_user) values ('$orderid','$pid','$q','$price','$date','$user')

相關問題