Catchable fatal error: Object of class PDOStatement could not be converted to string in line 114

后端 未结 3 631
南方客
南方客 2020-12-02 00:45

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/mand

相关标签:
3条回答
  • 2020-12-02 01:21
    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'];
    
            }
    
    $array = array( $max, $ProductID, $value, $price );
    
    $sql->prepare
        ("
        INSERT INTO Bestellingsdetail (Bestelnummer, ProductID, Aantal, Prijs)
        VALUES (?, ?, ?, ?)
        ")
    
    $sql->execute($array);
    
    }
    
    0 讨论(0)
  • 2020-12-02 01:25

    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.

    0 讨论(0)
  • 2020-12-02 01:37

    Try:

    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`, `Prij`s)
            VALUES ($max,$ProductID,$value,$price)";
            $smtp = $db->prepare($sql);
            $count = $smtp->execute();
    

    However, try and use the prepared statements as you are defeating the reason of using PDO and could be at risk of injection:

    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)";
            $stmt = $db->prepare($sql);
            $stmt->bindParam(':max', $max);
            $stmt->bindParam(':ProductID', $ProductID);
            $stmt->bindParam(':value', $value);
            $stmt->bindParam(':price', $price);
            $count = $smtp->execute();
    
    0 讨论(0)
提交回复
热议问题