无法使用PDO将int插入到mySql数据中

时间:2012-11-14 21:20:56

标签: php pdo

我正在尝试使用PDO将int插入到数据库中,但由于某种原因,它会因错误而失败:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2031 ' in C:\vhosts\jpl\admin\add_project.php:119 Stack trace: #0 C:\vhosts\jpl\admin\add_project.php(119): PDOStatement->execute() #1 C:\vhosts\jpl\admin.php(25): include('C:\vhosts\jpl\a...') #2 {main} thrown in C:\vhosts\jpl\admin\add_project.php on line 119

以下是代码:

        $projectAdd = $database->prepare("INSERT INTO projects (title, short_description, long_description, image_location, display)VALUES ('test', 'test', 'test', ?, :integer)");
        $projectAdd->bindParam(1, $test);
        $projectAdd->bindParam(':integer', $value, PDO::PARAM_INT);

        $test = "testbind";
        $value = 1;
        $projectAdd->execute();

即使我使用直接int,它仍然失败。 I.E 0而不是绑定。

*编辑在这个问题上犯了错误。

1 个答案:

答案 0 :(得分:4)

尝试:

$projectAdd = $database->prepare("INSERT INTO projects (title, short_description, long_description, image_location, display)VALUES ('test', 'test', 'test', ?, ?)");
//The variables need to be defined before binding.
$test = "testbind"; 
$testint = 1;
$projectAdd->bindParam(1, $test);
$projectAdd->bindParam(2, $testint); //change '1' to '2'