【问题标题】:Inserting Shopping cart Details Into MySQL Database Using PHP使用 PHP 将购物车详细信息插入 MySQL 数据库
【发布时间】:2011-11-08 18:29:50
【问题描述】:

我有一个购物车,此时它会将客户带来的商品发送到数据库,但现在我已经包含了一个登录系统,您必须在购买商品之前成为会员。我已将登录用户保留在会话中,因此一旦下订单,我也会尝试将会话变量发送到数据库。目前,我有三个表,分别是 customers、orders 和 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(); 
    }
?>

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

谢谢

【问题讨论】:

    标签: php mysql windows shopping-cart


    【解决方案1】:

    呃。完全没有错误处理的数据库操作。假设数据库查询成功只会让您陷入这种情况 - 不知道出了什么问题。

    绝对最低限度,您的数据库操作应如下所示:

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

    这至少可以阻止脚本死在其轨道上,告诉您查询失败,告诉您为什么失败,并告诉您查询是什么。

    同样,您的代码对SQL injection 攻击是广泛开放的。这在显然是电子商务设置的情况下尤其糟糕。我建议您立即关闭此系统,直到您有机会阅读此内容并堵住漏洞。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        尝试用这个替换:

        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')

        【讨论】:

          猜你喜欢
          • 2020-07-09
          • 2016-08-15
          • 1970-01-01
          • 2014-08-14
          • 2018-09-14
          • 2013-05-21
          • 2012-07-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多