Using PDO and failing to insert a record

Chris

I am having a slight issue adding a record into the database.

For some reason it is not adding the record into the database.

I have a form in HTML and serialize it with jQuery to use in an AJAX request. I know this works as I used to use it with the old mysql commands.

This is what I have in the insert.php file:

$ln=($_POST['lastname']);
$fn=($_POST['firstname']);
$dob=($_POST['dob']);
$un=($_POST['username']);

$a1=($_POST['address1']);
$a2=($_POST['address2']);
$town=($_POST['town']);
$county=($_POST['county']);
$pc=($_POST['postcode']);
$country=($_POST['country']);
$lat=($_POST['lat']);
$lng=($_POST['lng']);

$lp=($_POST['landline']);
$mp=($_POST['mobile']);
$e1=($_POST['email1']);
$e2=($_POST['email2']);

$web=($_POST['web']);
$notes=($_POST['notes']);
$fb=($_POST['fbid']);
$tw=($_POST['twitter']);
$cat=($_POST['cat']);

$it=($_POST['Time']); 
$idate=($_POST['Date']); 
$iip=($_POST['ipaddress']);
$ib=($_POST['browser']);
$ios=($_POST['os']);

These are the posted values being used from the form.

I then have the following queries (I have 5 different ones as they are writing to 5 tables. As I say, this worked with the old mysql commands, but not the PDO commands.

$sqlp = $conn->prepare("INSERT INTO ".PERSON." (lastname, firstname, dob, adbkid) VALUES(:ln, :fn, :dob, :un)");
$sqlp->execute();
$idp = $conn->lastInsertId();

$sqla = $conn->prepare("INSERT INTO ".ADDRESS." (address1, address2, town, county, postcode, country, lat, lng, personID) VALUES (:a1, :a2, :town, :county, :pc, :country, :lat, :lng, :un)");
$sqla->execute();
$ida = $conn->lastInsertId();

$sqlc = $conn->prepare("INSERT INTO ".CONTACT." (landline, mobile, email1, email2, personID) VALUES (:lp, :mp, :e1, :e2, :un)");
$sqlc->execute();
$idc = $conn->lastInsertId();

$sqlm = $conn->prepare("INSERT INTO ".MISC." (web, notes, photo, fbid, twitter, cat, personID) VALUES (:web, :notes, :pic, :fb, :tw, :cat, :un)");
$sqlm->execute();
$idm = $conn->lastInsertId();

$sqlv = $conn->prepare("INSERT INTO ".VARI." (Time, Date, ipaddress, browser, os, personID) VALUES (:it, :idate, :ip, :ib, :ios, :un)"); 
$sqlv->execute();
$idv = $conn->lastInsertId();

$sqlp->bindValue(':ln', $ln);
$sqlp->bindValue(':fn', $fn);
$sqlp->bindValue(':dob', $dob);
$sqlp->bindValue(':un', $un);

$sqla->bindValue(':a1', $a1);
$sqla->bindValue(':a2', $a2);
$sqla->bindValue(':town', $town);
$sqla->bindValue(':county', $county);
$sqla->bindValue(':pc', $pc);
$sqla->bindValue(':country', $country);
$sqla->bindValue(':lat', $lat);
$sqla->bindValue(':lng', $lng);

$sqlc->bindValue(':lp', $lp);
$sqlc->bindValue(':mp', $mp);
$sqlc->bindValue(':e1', $e1);
$sqlc->bindValue(':e2', $e2);

$sqlm ->bindValue(':web', $web);
$sqlm ->bindValue(':notes', $notes);
$sqlm ->bindValue(':pic', $pic);
$sqlm ->bindValue(':fb', $fb);
$sqlm ->bindValue(':tw', $tw);
$sqlm ->bindValue(':cat', $cat);

$sqlv ->bindValue(':it', $it);
$sqlv ->bindValue(':idate', $idate);
$sqlv ->bindValue(':iip', $iip);
$sqlv ->bindValue(':ib', $ib);
$sqlv ->bindValue(':ios', $ios);

I have ran these with a try catch to see if I can figure out what is going on and I get the following error:

"SQLSTATE[HY093]: Invalid parameter number: no parameters were bound"

I have echoed out what is being put into each of the values and it corresponds with what I have in entered into the form.

I am also trying to echo out the last inserted ID's for each table and it isn't echoing that out.

I have had a look around Google and php.net and here and from what I read I am doing everything as they say I should be.

I am at a loss as to what is happening.

If you had any pointers they would be most welcome.

chresse

you have to bind the params first and then execute it

i.e:

$sqlp = $conn->prepare("INSERT INTO ".PERSON." (lastname, firstname, dob, adbkid) VALUES(:ln, :fn, :dob, :un)");
$sqlp->bindValue(':ln', $ln);
$sqlp->bindValue(':fn', $fn);
$sqlp->bindValue(':dob', $dob);
$sqlp->bindValue(':un', $un);
$sqlp->execute();
$idp = $conn->lastInsertId();
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP PDO batch insert from multi dimensional array failing to insert into MySQL

From Dev

Unable to insert a record into MySQL database using DBI

From Dev

Using PDO and failing to insert a record

From Dev

MySQL INSERT record using SELECT

From Dev

get first insert id for multiple insert using pdo in mysql

From Dev

From Textarea to multiple value insert using PDO

From Dev

Insert Values with special Characters using PDO

From Dev

pdo insert statement using datetime errors

From Dev

Insert record PHP/PDO throwing error

From Dev

ADO insert a record into access DB using delphi

From Dev

PDO Insert operation using PHP and MYSQL not working

From Dev

Insert record with relationship using OData

From Dev

How to insert into database using PDO where columns to insert change?

From Dev

PDO insert with foreign key is failing

From Dev

PHP PDO batch insert from multi dimensional array failing to insert into MySQL

From Dev

Unable to insert into table using PDO in mysql

From Dev

Using mysql insert with implode in PDO

From Dev

How to insert compressed data using PDO?

From Dev

php pdo insert datas using function

From Dev

Form not inputting in database using PDO prepare and INSERT

From Dev

How to get the insert ID after insert using the PDO transactino in php

From Dev

unable to insert file name to sql using PDO

From Dev

Confirm record INSERT with PDO and MySQL

From Dev

Insert multiple rows using PHP PDO

From Dev

mysql pdo update record using LEFT JOIN

From Dev

how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

From Dev

insert database using pdo with datetime and auto increment

From Dev

INSERT SQL failing using Access with Azure linked tables

From Dev

Insert data with associative array using PDO

Related Related

  1. 1

    PHP PDO batch insert from multi dimensional array failing to insert into MySQL

  2. 2

    Unable to insert a record into MySQL database using DBI

  3. 3

    Using PDO and failing to insert a record

  4. 4

    MySQL INSERT record using SELECT

  5. 5

    get first insert id for multiple insert using pdo in mysql

  6. 6

    From Textarea to multiple value insert using PDO

  7. 7

    Insert Values with special Characters using PDO

  8. 8

    pdo insert statement using datetime errors

  9. 9

    Insert record PHP/PDO throwing error

  10. 10

    ADO insert a record into access DB using delphi

  11. 11

    PDO Insert operation using PHP and MYSQL not working

  12. 12

    Insert record with relationship using OData

  13. 13

    How to insert into database using PDO where columns to insert change?

  14. 14

    PDO insert with foreign key is failing

  15. 15

    PHP PDO batch insert from multi dimensional array failing to insert into MySQL

  16. 16

    Unable to insert into table using PDO in mysql

  17. 17

    Using mysql insert with implode in PDO

  18. 18

    How to insert compressed data using PDO?

  19. 19

    php pdo insert datas using function

  20. 20

    Form not inputting in database using PDO prepare and INSERT

  21. 21

    How to get the insert ID after insert using the PDO transactino in php

  22. 22

    unable to insert file name to sql using PDO

  23. 23

    Confirm record INSERT with PDO and MySQL

  24. 24

    Insert multiple rows using PHP PDO

  25. 25

    mysql pdo update record using LEFT JOIN

  26. 26

    how to make an insert function with in a database class to insert a new record with multiple coloumns and multiple values (using PDO )?

  27. 27

    insert database using pdo with datetime and auto increment

  28. 28

    INSERT SQL failing using Access with Azure linked tables

  29. 29

    Insert data with associative array using PDO

HotTag

Archive