Hello, can someone help with this code. Fatal error: Call to undefined method PDO::execute() in C:\xampp\htdocs\invoice\taxform.php on line 136 <?php $db = new PDO( 'mysql:dbname=homedb;hostname=localhost', 'root', ' ' ); $db->prepare(' INSERT INTO numbers ( taxrate ) VALUES ( :taxRate ) '); $db->execute(array( ':taxrate' => $rate )); // error points to this template_header('Tax Rate ' . ( $outputRate = htmlspecialchars($_POST['taxRate']) ) . ' Set'); echo ' <p> Rate successfully set to ',$outputRate, ' </p>'; template_footer(); die; ?>
You didn't assign the "prepare" to a statement -- you don't do "execute" on your PDO object, you do it on the PDOstatement returned BY the prepare. <?php $db = new PDO( 'mysql:dbname=homedb;hostname=localhost', 'root', ' ' ); $statement = $db->prepare(' INSERT INTO numbers ( taxrate ) VALUES ( :taxRate ) '); $statement->execute([':taxrate' => $rate]); template_header('Tax Rate ' . ( $outputRate = htmlspecialchars($_POST['taxRate']) ) . ' Set'); echo ' <p> Rate successfully set to ', $outputRate, ' </p>'; template_footer(); ?> Code (markup): See? Also if you're on PHP 5.4/newer, you don't need to waste time saying "array" anymore
PDO::prepare returns a PDOStatement object which has the execute method. $st = $db->prepare(...); $st->execute(...); PHP: Copy pasted from the first link i found on Google when searching for "Fatal error: Call to undefined method PDO::execute() in" http://stackoverflow.com/questions/6964192/pdoexecute-is-an-undefined-method-for-me