1

I'm trying to insert an int into the database using PDO, but for some reason it keeps failing with the error:

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

Here's the code:

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

Even if I use a direct int it still fails. I.E 0 instead of binding.

*Edit made an error on the question.

3
  • 5
    Why are trying to bind param 1 twice? Commented Nov 14, 2012 at 21:22
  • can you post the query? What happens if you try to execute it directly on the client? Commented Nov 14, 2012 at 21:22
  • 2
    Try changing $projectAdd->bindParam(1, $testint); to $projectAdd->bindParam(2, $testint); Commented Nov 14, 2012 at 21:23

1 Answer 1

4

Try:

$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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.