Insert record PHP/PDO throwing error

Liam

I'm trying to build a simple PHP record insert only I can't seem to get it to work, it keeps saying that I have an error in my syntax.

This isn't going to be on a live production server, this is for internal (my use) only so injection/attacks are not a concern.

Code:

<?php
$servername = "server";
$username = "un";
$password = "p";
$dbname = "db";

$title = $_POST['title'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$dob = $_POST['dob'];
$evetelephone = $_POST['eve-telephone'];
$daytelephone = $_POST['day-telephone'];
$occupation = $_POST['occupation'];
$email = $_POST['email'];
$hearabout = $_POST['hear-about'];
$consent = $_POST['consent'];
$readrules = $_POST['readrules'];
$injury = $_POST['injury'];
$questions = $_POST['questions'];
$gymrisk = $_POST['gymrisk'];
$rules  = $_POST['rules'];
$remove = $_POST['remove'];
$fullname = $_POST['fullname'];
$signature = 'test';/*$_POST['signature'];*/

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO o18-reg (id, title, first_name, surname, gender, addr, dob, eve_tel, day_tel, occupation, email_addr, how_heard, over_18, rules, injury, questions, gym_risk, agree_rules, right_remove, full_name, signature)
    VALUES ($title, $firstname, $surname, $gender, $address, $dob, $evetelephone, $daytelephone, $occupation, $email, $hearabout, $consent, $readrules, $injury, $questions, $gymrisk, $rules, $remove, $fullname, $signature)";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
} catch(PDOException $e) {
   echo $sql . "<br>" . $e->getMessage();
}

$conn = null;

?>

Error:

INSERT INTO o18-reg (id, title, first_name, surname, gender, addr, dob, eve_tel, day_tel, occupation, email_addr, how_heard, over_18, rules, injury, questions, gym_risk, agree_rules, right_remove, full_name, signature) VALUES (Mr, Liam, Gallagher, Male, 5 Street, 25/11/1990, 0883821953, 032714821953, designer, [email protected], google told me, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Liam Gallagher, test) SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-reg (id, title, first_name, surname, gender, addr, dob, eve_tel, day_tel, occup' at line 1

Funk Forty Niner

Firstly, that table name with the hyphen; MySQL figures you want to do math here.

$sql = "INSERT INTO o18-reg

Which translates to o18 minus reg

so wrap it in ticks

$sql = "INSERT INTO `o18-reg` ...

or rename it to be an underscore.

Then you'll be faced with another problem, the VALUES. You have what seems to be strings, so you'll need to quote those.

I.e.

VALUES ('$title', '$firstname', '$surname' ...

and do the same for the rest of the strings.

Then you're faced with an SQL injection.

Use a prepared statement:

Edit:

I just noticed, you have 21 columns, but using 20 variables without making the necessary adjustement to compensate. You would have received an additional error about columns do not match, something to the effect of:

Fatal error: Uncaught exception 'PDOException' with message. Insert value list does not match column list: 1136 Column count doesn't match value count at row 1

If id is an AI, you will need to make a slight adjustment in your query, by adding '' in:

VALUES ('', '$title', '$firstname' ...

If there is a variable set aside for it (which doesn't seem to be present in your posted code), then add it. However, if the id column is an AI, then use the '' above.


An insight:

Make sure that your form does use a POST method and that all inputs bear the name attributes, and with no typos.

  • They seem to be populating correctly, but this is also for future visitors to the question.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Function with insert mysqli throwing error

From Dev

mysql insert stored procedure throwing an error

From Dev

Insert into in python cql throwing syntax error

From Dev

insert_record(); Error writing to database

From Dev

How to solve timetuple error on insert record in dbf

From Dev

PLSQL Error when trying to insert record

From Dev

Getting Error in insert record in MySql from Python

From Dev

Unable to insert record into Sqlite from python, Syntax Error

From Dev

"Syntax error in INSERT INTO statement" when adding record to Access database

From Dev

trigger after on insert, error table mutation upon inserting record

From Dev

Unit Testing Insert Empty Record Should Throw Error

From Dev

Add textbox value to single record: syntax error in INSERT INTO statement

From Dev

Python client library for Google Calendar API v3 insert event throwing "Missing end time" error

From Dev

Serverless lambda function throwing error when trying to insert data into dynamoDB Table

From Dev

Entity Framework Throwing Cannot Insert Explicit Value for Identity Column In Table ... When IDENTITY_INSERT is set to OFF Error

From Dev

TermDocumentMatrix sometimes throwing error

From Dev

Mapreduce: NLineInputFormat throwing Error

From Dev

PIG setup throwing error

From Dev

SwitchCompat throwing error

From Dev

Post throwing 500 error

From Dev

Why is react throwing an error

From Dev

Bigquery throwing "Unexpected" error

From Dev

Sequelize validation throwing error

From Dev

Inputing Integers error throwing

From Dev

Throwing CInternetException for error 12029

From Dev

performSegueWithIdentifier throwing strange error

From Dev

Jquery Draggable Throwing error

From Dev

PhpStorm Is throwing an error for phpcs

From Dev

Throwing an error in recursive function

Related Related

  1. 1

    Function with insert mysqli throwing error

  2. 2

    mysql insert stored procedure throwing an error

  3. 3

    Insert into in python cql throwing syntax error

  4. 4

    insert_record(); Error writing to database

  5. 5

    How to solve timetuple error on insert record in dbf

  6. 6

    PLSQL Error when trying to insert record

  7. 7

    Getting Error in insert record in MySql from Python

  8. 8

    Unable to insert record into Sqlite from python, Syntax Error

  9. 9

    "Syntax error in INSERT INTO statement" when adding record to Access database

  10. 10

    trigger after on insert, error table mutation upon inserting record

  11. 11

    Unit Testing Insert Empty Record Should Throw Error

  12. 12

    Add textbox value to single record: syntax error in INSERT INTO statement

  13. 13

    Python client library for Google Calendar API v3 insert event throwing "Missing end time" error

  14. 14

    Serverless lambda function throwing error when trying to insert data into dynamoDB Table

  15. 15

    Entity Framework Throwing Cannot Insert Explicit Value for Identity Column In Table ... When IDENTITY_INSERT is set to OFF Error

  16. 16

    TermDocumentMatrix sometimes throwing error

  17. 17

    Mapreduce: NLineInputFormat throwing Error

  18. 18

    PIG setup throwing error

  19. 19

    SwitchCompat throwing error

  20. 20

    Post throwing 500 error

  21. 21

    Why is react throwing an error

  22. 22

    Bigquery throwing "Unexpected" error

  23. 23

    Sequelize validation throwing error

  24. 24

    Inputing Integers error throwing

  25. 25

    Throwing CInternetException for error 12029

  26. 26

    performSegueWithIdentifier throwing strange error

  27. 27

    Jquery Draggable Throwing error

  28. 28

    PhpStorm Is throwing an error for phpcs

  29. 29

    Throwing an error in recursive function

HotTag

Archive