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

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

can i ask youu what this line of code is doing

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

and this one as well if($stmt->fetch() != NULL)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.