swager101
swager101

Reputation: 3

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I'm getting an

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined" ... PDOStatement->execute() #1 {main} thrown on line 61. Line 61 being $stmt->execute();

$add_joke = filter_input(INPUT_POST, 'add_joke');

if ($add_joke != NULL){
    $actual_date = date('Y-m-d');
    $actual_id = null;

    $sql = 'INSERT INTO joke (id, JokeText, Joke Date) VALUES (:id, :JokeText, :Joke Date)';
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':id', $actual_id);
    $stmt->bindParam(':JokeText', $add_joke);
    $stmt->bindParam(':Joke Date', $actual_date);
    $stmt->execute();

}

Upvotes: 0

Views: 249

Answers (2)

Anatoliy R
Anatoliy R

Reputation: 1789

Are you using MySQL? Anyway, try to change code for actual_id: change it to 0 or remove it from query and specify it as an auto increment primary key in your table. If you really meant to add NULL value (for any reason), just remove it form query and make table field nullable.

Also not sure if you can use space in ':Joke Date'

Finally, not a great idea to use anything like

if ($add_joke != NULL)

in PHP code

Upvotes: 1

TimBrownlaw
TimBrownlaw

Reputation: 5507

The Likely issue is your use of 'Joke Date' which has a space in it.

Your naming seems a little confused. You can either change Joke Date to JokeDate or Joke_Date or joke_date or simply date or date_created depending upon your naming conventions.

Upvotes: 1

Related Questions