The problem is that you’re calling execute() on the PDO object. The prepare() method returns a PDOStatement object, and you have to call execute() on that:
Notice: Undefined variable: pdo in C:\xampp\htdocs\invoice\ axform.php on line 139
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\invoice\ axform.php on line 139
<?php
// Create the database connection as a PDO object:
try
{$db_options = array(
PDO::ATTR_EMULATE_PREPARES => false // important! use actual prepared statements (default: emulate prepared statements)
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // fetch associative arrays (default: mixed arrays)
);
$db = new PDO('mysql:host=localhost;dbname=homedb;charset=utf8', 'root', 'cookie');}
catch (PDOException $e)
{ // Report the Error!
echo "Something is not right, check your php.ini settings or code<br>";}
$query = 'UPDATE numbers SET taxrate = :taxrate WHERE id = :id';
// Prepare the Statement:
$stmt = $pdo->prepare($query);
// execute the statement:
$result = $stmt->execute(array(':taxrate' => $rate, ':id' => $id)); // primary key in the table column.
if ($result) { echo "Success!"; }
else { echo "Not again"; } // Failure
?>
That’s because in this code you’ve just posted you’ve assigned the PDO connection to the variable $db and then lower down you’re trying to call [B]$pdo[/B]->prepare instead of [B]$db[/B]->prepare
Edit: I also notice that you’re defining an options array, but you’re not actually passing it to the PDO constructor. You need to do this:
$db = new PDO('mysql:host=localhost;dbname=homedb;charset=utf8', 'root', 'cookie', $db_options);
Notice: Undefined variable: rate in C:\xampp\htdocs\invoice\ axform.php on line 138
Notice: Undefined variable: id in C:\xampp\htdocs\invoice\ axform.php on line 138
Success!
with this code:
<?php
// Create the database connection as a PDO object:
try
{ $db_options = array(
PDO::ATTR_EMULATE_PREPARES => false // important! use actual prepared statements (default: emulate prepared statements)
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // fetch associative arrays (default: mixed arrays)
);
$db = new PDO('mysql:host=localhost;dbname=homedb;charset=utf8', 'root', 'cookie', $db_options); }
catch (PDOException $e)
{ // Report the Error!
echo "Something is not right, check your php.ini settings or code<br>";}
$query = 'UPDATE numbers SET taxrate = :taxrate WHERE id = :id';
// Prepare the Statement:
$stmt = $db->prepare($query);
// execute the statement:
$result = $stmt->execute(array(':taxrate' => $rate, ':id' => $id)); // primary key in the table column.
if ($result) { echo "Success!"; }
else { echo "Not again"; } // Failure
?>
Only driver specific (i.e. mysql specific) options can be passed in the constructor. To set thing like the error handler requires using the setAttribute method. Just one of those things.