0

I've created php ekart site just for testing purpose. My query is when I click on add to cart it should firstly validate whether any one color and any one size is selected, if both have been selected it adds product to the cart this works fine but if I add same product but with different color or size it overlaps the same product which was already added and that products quantity turns to 1 but if I select another product it works well but same thing goes when I select different size or color. Since I am a beginner at php, I'm not sure where am I going wrong here.

Php code of Product add to cart is given Below:

$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>";
    }
}

Cart program code

<?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 structure given below: Image of database structure Image of database structure

Hardik Sisodia
  • 615
  • 3
  • 14
  • 37
  • since you are new to PHP, it is a good time to move over to mysqli functions or pdo. Mysql_* functions are deprecated and easily subject yourself to sql injection, read [How can I prevent sql injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Drew Aug 19 '15 at 12:49
  • @Drew Thank You for sharing this useful information, I will surely go ahead and learn PDO and Mysqli function. Well do you have any clue where am I going wrong currently with my above problem? – Hardik Sisodia Aug 20 '15 at 10:17

1 Answers1

1

Use this code

<?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);
    $index = md5($id.$color.$size);
    if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
        $_SESSION['cart'][$index]['quantity']++;
        echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
    }else{
        $_SESSION['cart'][$index] = 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>";
    }
}

?>
Zebi Rajpot
  • 186
  • 5
  • I tried doing it but it neither adds quantity to added product nor does it create other line of newly added product – Hardik Sisodia Aug 20 '15 at 10:00
  • i updated code please check updated post and replace line with new code ... it will work – Zebi Rajpot Aug 20 '15 at 11:21
  • That worked well but when I tried adding same product again but this time with different size and color it overlapped the previous size and color of the same product instead of adding a new entry of same product with different size and color – Hardik Sisodia Aug 20 '15 at 11:38
  • I copy pasted the code but problem is still the same product adds very well it increases quantity if same size and color has been selected but if I select different size and color of same product it overlaps the session instead of creating a new entry in the cart – Hardik Sisodia Aug 20 '15 at 11:51
  • because you are using same value in $id variable ... try use different $id values for each entry – Zebi Rajpot Aug 20 '15 at 11:58
  • Actually `$id` is different only but `$cat` is same because cat defines category which might be same for many products – Hardik Sisodia Aug 20 '15 at 12:01
  • its not different try and check $id value ... array overwrites because scripts checking hey,,, is $id item exists on now if color is same and size same increment quantity if not then add new product item... and when it goes to else condition because of $id value same so $_SESSION['cart'][$id] is getting overwrite .. – Zebi Rajpot Aug 20 '15 at 12:06
  • In my database, when I add any product to display on public page, I submit all size and color at once so in my database it shows all size which are added and all color with name and other details in same row. So, Yes each size and color will be having one $id of same product and other product has different id but in database I have not created a new entry for all different size and color, its all stored in one row per product. – Hardik Sisodia Aug 20 '15 at 12:26
  • so you can add this line to separate each product in array with color and size here is example $_SESSION['cart'][$id.$color.$size] = array('quantity' => 1, ... and continue .. getting ? – Zebi Rajpot Aug 20 '15 at 14:36
  • I am not getting it properly actually can you edit the code so that I can understand please. – Hardik Sisodia Aug 20 '15 at 18:29
  • i updated the code copy paste it will work .. i changed the id variable with $index = md5($id.$color.$size); – Zebi Rajpot Aug 21 '15 at 08:19
  • I am now getting this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in B:\XAMPP\htdocs\ekart\viewcart\cart.php on line 139 and on line 139 I have this code: while($row=mysql_fetch_array($data)) I have updated my cart code above please do have a look is there any problem with cart too? – Hardik Sisodia Aug 21 '15 at 08:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87568/discussion-between-zebi-rajpot-and-hardik-sisodia). – Zebi Rajpot Aug 21 '15 at 09:02
  • I tried alot but not getting any solution regards to my query – Hardik Sisodia Aug 25 '15 at 09:43