help w/pdo

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]<?php

$db = new PDO(
‘mysql:dbname=homedb;hostname=localhost’,
‘root’, ‘cookie’
);
$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 ’

Rate successfully set to ',$outputRate, '

'; template_footer(); die; ?> [/php]

[php]
$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)
);

// The above not truly needed, I believe newer versions of PHP take care of the problem. But it wouldn’t hurt.

$db = new PDO(‘mysql:dbname=homedb;hostname=localhost’,‘root’,‘cookie’, $db_options );
$stmt = $db->prepare(‘INSERT INTO numbers (taxrate) VALUES (:taxRate)’);
$result = $stmt->execute(array(’:taxrate’ => $rate )); // error points to this

if ($result) {
// Success … OK TO CONTINUE WITH CODE:
} else {
// Failure
}[/php]

An when your query statements get complex, you might consider doing something like this:

[php]$db = new PDO(‘mysql:dbname=homedb;hostname=localhost’,‘root’,‘cookie’ );
$query = ‘INSERT INTO numbers (taxrate) VALUES (:taxRate)’;
$stmt = $db->prepare($query);
$result = $stmt->execute(array(’:taxrate’ => $rate )); // error points to this

if ($result) {
// Success … OK TO CONTINUE WITH CODE:
} else {
// Failure
}[/php]

here is the result:

Notice: Undefined variable: rate in C:\xampp\htdocs\invoice\taxform.php on line 130

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY093]:
Invalid parameter number: parameter was not defined’ in
C:\xampp\htdocs\invoice\taxform.php:130 Stack trace: #0
C:\xampp\htdocs\invoice\taxform.php(130): PDOStatement->execute(Array) #1 {main}
thrown in C:\xampp\htdocs\invoice\taxform.php on line 130

[php]<?php
$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”, “root”, “cookie”);
$db = new PDO(‘mysql:dbname=homedb;hostname=localhost’,‘root’,‘cookie’, $db_options );
$stmt = $db->prepare(‘INSERT INTO numbers (taxrate) VALUES (:taxRate)’);
$result = $stmt->execute(array(’:taxrate’ => $rate )); // error points to this

if ($result) {
// Success … OK TO CONTINUE WITH CODE:
} else {
// Failure
}
?>

[/php]

hmm? (Me thinking ;D)

cross that I think I spotted the problem:
[php]$stmt = $db->prepare(‘INSERT INTO numbers (taxrate) VALUES (:taxrate)’);[/php]

taxrate instead of taxRate :wink:

Thanks for the help; I just can’t adjust to pdo although it’s got to be better than
mysqli. please look at this again. I thought Rate was defined. Question - the NUMBERS table has 4 fields and is
used by multiple files which update one of the fields (receipt#, etc.). I’m wondering
if I shouldn’t be updating.

Notice: Undefined variable: rate in C:\xampp\htdocs\invoice\taxform.php on line 125
[php]<?php $db = new PDO('mysql:dbname=homedb;hostname=localhost','root','cookie' ); $query = 'INSERT INTO numbers (taxrate) VALUES (:taxrate)'; $stmt = $db->prepare($query); $result = $stmt->execute(array(':taxrate' => $rate )); // error line

if ($result) {
// Success … OK TO CONTINUE WITH CODE:
} else {
// Failure
}
?>[/php]

taxrate has to be exactly spelled the same way in the code as it is in the mysql database table and make sure the database table name is exactly what you have in the code, as for updating. If you are just changing (updating) a current value of tax rate then yes you should be updating.

maybe something like:

[php]// Update the edited text:
$query = ‘UPDATE numbers SET taxrate = :taxrate WHERE receipt = :receipt’;

// Prepare the Statement:
$stmt = $pdo->prepare($query);

// execute the statement:
$result = $stmt->execute(array(’:taxrate’ => $rate, ‘:receipt’ => $receipt)); // Usually it’s id, but I’m taking a guess with receipt but what ever you primary key in the table column. [/php]

I also would change
[php]$db = new PDO(‘mysql:dbname=homedb;hostname=localhost’,‘root’,‘cookie’ );[/php]
to

[php]$this->_connection = new PDO(‘mysql:host=localhost;dbname=homedb;charset=utf8’, ‘root’, ‘cookie’);[/php]

to see if that helps you out any (I’m thinking unlikely, but it wouldn’t hurt ;)):

Plus, you might to put back the $db_options code back in to see if that helps you out? (Nothing venture, nothing lost).

P.S. are you sure $rate is defined in php?

I was also thinking if you can look how you table is structure something like this (if you can get a sql output of it):

Just an example:
[php]CREATE TABLE IF NOT EXISTS $table (

   `id` int(11) NOT NULL AUTO_INCREMENT,

   `username` varchar(30) NOT NULL,

   `password` char(60) NOT NULL,

   `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

   PRIMARY KEY (`id`)

	 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;" ;

[/php]

It might help you out (an maybe others too) in debugging your problem?

How a litte file can be so difficult.
with - $this->_connection = new PDO(‘mysql:host=localhost;dbname=homedb;charset=utf8’, ‘root’, ‘cookie’); - I get:

Fatal error: Using $this when not in object context in C:\xampp\htdocs\invoice\taxform.php on line 127
with - $db = new PDO('mysql:dbname=homedb;hostname=localhost','root','cookie' ); - I get:
Notice: Undefined variable: pdo in C:\xampp\htdocs\invoice\taxform.php on line 131 Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\invoice\taxform.php on line 131

[php]<?php
$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:dbname=homedb;hostname=localhost’,‘root’,‘cookie’ );
// $this->_connection = new PDO(‘mysql:host=localhost;dbname=homedb;charset=utf8’, ‘root’, ‘cookie’);

$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.
// $query = ‘INSERT INTO numbers (taxrate) VALUES (:taxrate)’;
// $stmt = $db->prepare($query);
// $result = $stmt->execute(array(’:taxrate’ => $rate ));

if ($result) { echo “Success!”; }
else { echo “Not again”; } // Failure
?>

[/php]

me bad, I just copied the connection string:

[php]$this->_connection = new PDO(‘mysql:host=localhost;dbname=homedb;charset=utf8’, ‘root’, ‘cookie’);[/php]

should be

[php]$db = new PDO(‘mysql:host=localhost;dbname=homedb;charset=utf8’, ‘root’, ‘cookie’);[/php]

Which brings up a good point in a way, when debugging look over the details in the code, the unfortunate part is I can’t test it out.

This might help, write the following code; however, you will have to change it to your code variables :

[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)

	   ); 		 

$pdo = new PDO('mysql:host=localhost;dbname=demo_login_system;charset=utf8', 'root', 'your_password', $db_options);	 

} catch (PDOException $e) { // Report the Error!

echo "Something is not right, check your php.ini settings or code<br>";

} [/php]

Hint, I believe only line that will need changing is line 14. :wink:

Use this as a part of your code to connect to the database and maybe this will help you debug & hopefully get it to execute.

I’m sorry, I must have missed something. I still get:

Notice: Undefined variable: pdo in C:\xampp\htdocs\invoice\taxform.php on line 139

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\invoice\taxform.php on line 139

[php]<?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
”;}

$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

?>[/php]

Change this: [php]$stmt = $pdo->prepare($query)[/php] to [php]$stmt = $db->prepare($query)[/php]

As my boss would had said “John, you weren’t paying attention to details”> ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service