0

我有一个 pdo 块,用于将值插入到我的表中,如下所示

try{

        $user = 'root';
        $pass = null;
        $pdo = new PDO('mysql:host=localhost; dbname=divebay', $user, $pass);

        $name = $_POST['name'];
        $desc = $_POST['description'];
        $cond = $_POST['condGroup'];
        $sprice = $_POST['sprice'];
        $iprice = $_POST['iprice'];
        $incprice = $_POST['incprice']; 
        $duration = $_POST['duration'];
        $img = $_POST['img'];


        $owner = $_SESSION['username'];

        $valid = "set";
        $stmt2 = $pdo->prepare("SELECT * FROM auction WHERE ID = :id");
        $stmt2->bindParam(":id", $random, PDO::PARAM_INT);
        while(isset($valid)){
            $random = rand(100000,999999);
            $stmt2->execute();

            if(!$stmt2->fetch(PDO::FETCH_ASSOC)){
                unset($valid);
            }
        }

        $timestamp = time() + ($duration * 24 * 60 * 60);

        $stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description)
                      VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description");
        $stmt->bindParam(':id', $random, PDO::PARAM_INT);
        $stmt->bindParam(':name', $name, PDO::PARAM_STR);
        $stmt->bindParam(':owner', $owner, PDO::PARAM_STR);
        $stmt->bindParam(':holder', $owner, PDO::PARAM_STR);
        $stmt->bindParam(':iprice', $iprice, PDO::PARAM_STR);
        $stmt->bindParam(':sprice', $sprice, PDO::PARAM_STR);
        $stmt->bindParam(':incprice', $incprice, PDO::PARAM_STR);
        $stmt->bindParam(':etime', $timestamp, PDO::PARAM_INT);
        $stmt->bindParam(':img', $img, PDO::PARAM_STR);
        $stmt->bindParam(':condition', $condition, PDO::PARAM_STR);
        $stmt->bindParam(':description', $description, PDO::PARAM_STR);
        if($stmt->execute()){
            $worked ="yes";
        }

}catch(PDOException $e){
        echo $e->getMessage();
}

我不知道为什么这个语句不会执行,运行脚本时没有设置 $worked 变量。所有数据库列名和数据类型都已按原样检查正确。我从来没有遇到过直到现在才执行的语句的问题。怎么了?我该如何调试呢?

4

2 回答 2

4

如果您使用错误模式异常设置数据库连接,如果您的语句有问题,PDO 将引发异常。我还看到您正在为 PDO 使用 MySQL 驱动程序。如果你这样做,你应该总是禁用模拟的准备好的语句。所以我会写你的连接如下(注意我也设置了编码):

$pdo = new PDO('mysql:host=localhost; dbname=divebay;charset=utf8', $user, $pass);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

另请参阅此帖子以获取有关此内容的更多信息。

一旦你这样做了,你会发现你的陈述是错误的。)您在声明的末尾缺少一个:

$stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description)
                       VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description)");
                                                                                                                                ^
于 2012-09-11T17:27:50.813 回答
1

修改这一行:

$stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description)
                      VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description");

$stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description)
                      VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description)");

区别在于)最后。

并告诉我它现在是否有效。

于 2012-09-11T16:59:04.970 回答