仅当产品在相同颜色和尺寸的购物车中可用时,才能在php中将产品数量添加到购物车

时间:2015-08-19 12:46:41

标签: php jquery mysql session

我已经创建了php ekart网站,仅用于测试目的。我的查询是,当我点击添加到购物车时,它应首先验证是否选择了任何一种颜色和任何一种尺寸,如果两者都被选中,它将产品添加到购物车这工作正常,但如果我添加相同的产品但颜色不同或大小它与已经添加的相同产品重叠,产品数量变为1但是如果我选择另一种产品它运作良好但是当我选择不同尺寸或颜色时同样的事情。由于我是php的初学者,我不确定我在哪里出错。

产品添加到购物车的Php代码如下:

$sql = "SELECT * FROM allprods WHERE listing = '$listing' and  id = '$id'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 1)
{
    $row = mysql_fetch_array($data);
    if (isset($_SESSION['cart'][$id]))
    {
        if(isset($_SESSION['cart'][$id][$color]) && isset($_SESSION['cart'][$id][$size]))
        {
            $_SESSION['cart'][$id]['quantity']++;
            echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
        }
        else
        {
            $_SESSION['cart'][$id] = array('quantity' = >1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
            echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
        }
    }
    else
    {
        $_SESSION['cart'][$id] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);

        echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
    }
}

购物车程序代码

<?php
    if(isset($_POST['qty']))
    {
        foreach($_POST['qty'] as $product_id=>$item_qty)
        {
            $id=(int)$product_id;
            $qty=(int)$item_qty;
            if($qty==0)
            {
                unset($_SESSION['cart'][$id]);
            }
            elseif($qty>0)
            {
                $_SESSION['cart'][$id]['quantity']=$qty;
            }
        }
    }
    else
    {
        echo "";
    }
    if(!empty($_SESSION['cart']))
    {
        require "../link_db.php";
        $sql="SELECT * FROM allprods WHERE id IN(";
        foreach($_SESSION['cart'] as $id=>$value)
        {
            $sql.=$id.',';
        }
        $sql=substr($sql,0,-1).') ORDER BY product_id ASC'; 
        $data=mysql_query($sql);
        while($row=mysql_fetch_array($data))
        {
?>
Table row and data goes here!!
<?php
        }
        else
        {
?>
<tr>
    <th colspan='4' class='noprodavailablewrap'>
        There are no products in your cart.
    </th>
</tr>
<?php
    }
?>

DB结构如下: Debugging LoadLibrary Failures Image of database structure

1 个答案:

答案 0 :(得分:1)

使用此代码

public function testIfCorrectCredentialsAreAccepted()
{
    $credentials = [ '_token' => csrf_token(), 'username' => 'test', 'password' => 'test' ];

    $user = User::create([ 'username' => $credentials['username'], 'firstname' => 'Test', 'surname' => 'Test' ]);

    $this->call('POST', '/auth/login', $credentials);
    $this->assertTrue(\Auth::check());

    DB::table('users')->where('username', $user->username)->delete();
}