0

Hello

I am trying to update the quantity value but I am not able to do it. As soon as the user clicks on Add To Basket, if the items exists I want it to update the value. At the moment, the quantity is one.

//so this bit checks if the item is already there

if ('SELECT `NameOfTheDVD`, `Qty` FROM `basket` WHERE `NameOfTheDVD` = '.$DVDname.'' ) {


        $Update = 'UPDATE `basket`
                  SET Qty = Qty + 1
                  WHERE `NameOfTheDVD` = '.$DVDname.'';

        $prepare = $dbhandle->prepare( $Update );
        $prepare -> execute();
    } else {
    //if the item does not exist it just add the item
    $query_insertintotable= 'INSERT INTO `basket` (DVDID, NameOfTheDVD, Qty, Price) 
                                 VALUES ('.$myid.',\''.$DVDname.'\',1,'.$price.')';
        echo $query_insertintotable;
        $query = $dbhandle->prepare( $query_insertintotable );


        //$result = mysql_query($query_insertintotable);
        $query->execute();
    }

The other thing I dont know how to increment the value.

The problem is also with the if statement, I am not able to add any of the products even if it does not exist in the database

I hope you guys can help me

Thank you

2
Contributors
3
Replies
15
Views
3 Years
Discussion Span
Last Post by Mohamed_26
0

Your if statement is not going to work.. You need to execute your Select statement. After you execute that select statement you can try to fetch data from the result set. You can read more about Fetch here

$Query = "SELECT NameOfTheDVD`, `Qty` FROM `basket` WHERE `NameOfTheDVD` = ?";
$stmt = $dbhandle->prepare($query);
$stmt->bind_param("s", $DVDname); 
$stmt->execute();

if($stmt->fetch() != NULL)
{
    Update = "UPDATE `basket`
                  SET Qty = Qty + 1
                  WHERE `NameOfTheDVD` = '?'";
        $prepare = $dbhandle->prepare( $Update );
        $prepare->bind("?",$DVDname);
        $prepare -> execute();
}else{
    //if the item does not exist it just add the item
    $query_insertintotable= "INSERT INTO `basket` (DVDID, NameOfTheDVD, Qty, Price) VALUES (?,?,?,?)";

        $query = $dbhandle->prepare( $query_insertintotable );
        $query->bind("isis",$myid,$DVDname,1,$price);
        $query->execute();
}

i hope this helps you

0

can i ask youu what this line of code is doing

$stmt->bind_param("s", $DVDname);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.