I'm trying to add some data to my database, but I'm getting the error Catchable fatal error: Object of class PDOStatement could not be converted to string in /var/www/mandje.php on line 114. This is the code I'm using:
foreach($_SESSION["cart"] as $id => $value){
$query = $db->query('SELECT * FROM Producten WHERE ProductID ="'.$id.'" ');
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$price = $row['Prijs'];
$ProductID = $row['ProductID'];
}
$sql="INSERT INTO Bestellingsdetail( Bestelnummer, ProductID, Aantal, Prijs)
VALUES ($max,$ProductID,$value,$price)"; //<---- line 114
$count = $db->execute($sql);
I don't really get what's going wrong here. Any help would be much appreciated :)
In the comments, you show the following:
$query = $db->query('SELECT MAX( Bestelnummer ) FROM Bestellingsdetail');
$query->execute();
$max = $query;
$max++;
This is not how you get the result from a query. You are setting $max
to a PDOStatement
object. You need to fetch()
the result in order to use it.
// I've added "AS maxval" to make it easier to get the row
$query = $db->query('SELECT MAX(Bestelnummer) AS maxval FROM Bestellingsdetail');
$max_row = $query->fetch(PDO::FETCH_ASSOC);
$max = $max_row['maxval'];
$max++;
Docs: http://www.php.net/pdo.query
P.S. $query->execute();
is only needed for prepared statements. query()
will execute the query immediately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With