可捕获的致命错误:类PDOStatement的对象无法在第114行转换为字符串

时间:2014-01-30 18:04:05

标签: php pdo

我正在尝试向我的数据库添加一些数据,但是我收到了错误Catchable致命错误:PDOStatement类的对象无法在第114行的/var/www/mandje.php中转换为字符串。 这是我正在使用的代码:

 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);

我真的不知道这里出了什么问题。任何帮助将不胜感激:))

3 个答案:

答案 0 :(得分:6)

在评论中,您显示以下内容:

$query = $db->query('SELECT MAX( Bestelnummer ) FROM Bestellingsdetail');
$query->execute();
$max = $query;
$max++;

这不是您从查询中获取结果的方式。您正在将$max设置为PDOStatement个对象。您需要fetch()结果才能使用它。

// 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++;

文档:http://www.php.net/pdo.query

P.S。准备好的陈述只需要$query->execute();query()将立即执行查询。

答案 1 :(得分:0)

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);

}

答案 2 :(得分:-1)

尝试:

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();

然而,尝试使用准备好的陈述,因为您要弄清楚使用PDO的原因并且可能存在注射风险:

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();