0

Sorry very new to PHP and mySQL.

Here is the code there error is referring to.

$query = 'INSERT INTO movies
            (title, year, actor, notes, category)
          VALUES
            (:code, :name, :price, :notes, :category_id)';
$statement = $db->prepare($query);
$statement->bindValue(':title', $code);
$statement->bindValue(':year', $name);
$statement->bindValue(':actor', $price);
$statement->bindValue(':notes', $notes);
$statement->bindValue(':category', $category_id);
$statement->execute();
$statement->closeCursor();

The error is refering to the execute(); statement Any help would be great.

1 Answers1

0

I think it should be like this with proper variable binding, you've used wrong names while doing it. Let's do it-

$query = 'INSERT INTO
            movies(title, year, actor, notes, category)
          VALUES 
            (:code, :name, :price, :notes, :category_id)';

$statement = $db->prepare($query);
$statement->bindValue(':code', $code); // see the changes here 
$statement->bindValue(':name', $name); // see the changes here 
$statement->bindValue(':price', $price); // see the changes here 
$statement->bindValue(':notes', $notes);// see the changes here  
$statement->bindValue(':category_id', $category_id); // see the changes here 
$statement->execute();
$statement->closeCursor();

PDOStatement::bindValue — Binds a value to a parameter.

parameter

Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.

value

The value to bind to the parameter.

But in your case you used different names while using bindValue(). See more http://php.net/manual/en/pdostatement.bindvalue.php

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • I am working on a project from class and pulled this from a different project that had different titles. would the changes you made to ':code'. $code); still work with the above statement of insert into movies(title, year, actor, notes, category) or would I need to change them to code, name, price, etc as well? – jriding Oct 17 '18 at 01:47
  • @jriding mate I didn't got your point properly. Let me edit my answer and add more verbose explanation to make it more clearer to you. – A l w a y s S u n n y Oct 17 '18 at 01:50
  • again thank you for your time and explanation. I am thinking I may have to go thru all of the code and make sure everything matches to what the database columns are so I do not make mistakes from misspellings. – jriding Oct 17 '18 at 01:56
  • I believe I understand what the reference url was saying. I will go thru my code and clean it up and if I still have issue will re post. Thanks again for the information. – jriding Oct 17 '18 at 02:04