2011-11-08 140 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')

相关问题