PDO Insert Query not running - and no error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

PDO Insert Query not running - and no error

Post by simonmlewis »

This script is not running anything. It' snot doing any inserts, and not throwing up any errors.
How do I identify why is it not inserting anything?
If the db name or fields were incorrect, it would stop and through an error. It isn't.

Code: Select all

$today = (date('Y-m-d'));

// random code to be added to email for clarification
$random = (rand()%99999999);

			$query = $pdo->query("INSERT INTO admin (firstname, lastname, email, created, password, address1, address2, town, county, postcode, `type`, telephone, drivinglicense, shift, status) VALUES (:firstname, :lastname, :email, :today, :password, :address1, :address2, :town, :county, :postcode, :user, :telephone, :drivinglicense, :shift, :random)");
			
      $result = $pdo->prepare($query);
      
      $result->execute(array(':firstname' => $firstname, ':lastname' => $lastname, ':email' => $email, ':today' => $today, ':password' => $password, ':address1' => $address1, ':address2' => $address2, ':town' => $town, ':county' => $county, ':postcode' => $postcode, ':user' => $user, ':telephone' => $telephone, ':drivinglicense' => $drivinglicense, ':shift' => $shift, ':random' => $random));
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PDO Insert Query not running - and no error

Post by requinix »

What $result successfully prepared? What does ->execute() return? What do you have the PDO error reporting setting as?
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PDO Insert Query not running - and no error

Post by Celauran »

Hang on. PDO::query executes the query. You're then passing the results of that to PDO::prepare. Skip the first step and just pass the query string to PDO::prepare.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PDO Insert Query not running - and no error

Post by simonmlewis »

Yes I just noticed that too. I've got two of them. It's sorted now thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply