使用PHP将购物车详细信息插入MySQL数据库

时间:2011-11-08 18:29:50

标签: php mysql windows shopping-cart

我有一个购物车,此时此刻将客户带来的物品发送到数据库,但现在我已经包含了一个登录系统,您必须在购买物品之前成为会员。我已将登录用户保留在会话中,因此我尝试将会话变量发送到数据库,一旦订单生成。目前,我有三个表,即客户,订单和order_detail(参见以下代码):

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(); 
    }
?>

上面的代码没有在我的订单表中插入任何内容。

由于

3 个答案:

答案 0 :(得分:0)

在mysql_query函数之后尝试or die(mysql_error())。这可能会为您提供有关该问题的更多信息......

答案 1 :(得分:0)

唉。数据库操作完全没有错误处理。假设数据库查询成功,只会让你进入这样的情况 - 没有任何关于错误的线索。

在绝对最小的情况下,您的数据库操作应该如下所示:

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

至少会阻止脚本停止运行,告诉您查询失败,告诉您它为什么失败,并告诉您查询是什么。

同样,您的代码 WIDE OPEN SQL injection次攻击。这显然是一个电子商务设置尤其糟糕。我建议你立即关闭这个系统,直到你有机会阅读并插上这些漏洞。

答案 2 :(得分:0)

请确保您的查询是否包含在每个值

尝试替换为:

function getCount(evt, length, char) { var lengthCount = evt.value.length; if (lengthCount > length) { this.value = this.value.substring(0, length); var charactersLeft = length - lengthCount + 1; } else { var charactersLeft = length - lengthCount; } var outputElement = document.getElementById(char); outputElement.innerHTML = charactersLeft; }

并确保表insert into order values ('$orderid','$pid','$q','$price','$date','$user')在未指定时没有其他非空字段:

order

相关问题