Reputation: 21
I have searched for an answer on numerous sites, however it seems like no one have had the same problem as I am having right now, the ones I have come across.
The issue is that am getting this error message:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '), NULL)' at line 2
The beginning of my code looks like this. I have been staring at it but I don't seem to find the problem, any help?
<?php
$dbhost="host"; <-- this is line 2.
$dbname="db";
$dbusername="user";
$dbpassword="pass";
ini_set("error_reporting", E_ALL);
if(isset($_POST) && !empty($_POST["namn"])) {
try {
$link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $link->prepare("INSERT INTO `eventcustomers` (`namn`,`enamn`,`datum`,`personnr`,`kon`,`telefon`,`epost`,`allergier`,`kategori`,`brodtext`,`reseto`,`eid`)
VALUES(:namn, :enamn, NOW(), :personnr, :kon, :telefon, :epost, :allergier, :kategori, :brodtext, time(), :eid)");
$stmt->bindParam(':namn', $_POST["namn"]);
$stmt->bindParam(':enamn', $_POST["enamn"]);
$stmt->bindParam(':personnr', $_POST["personnr"]);
$stmt->bindParam(':kon', $_POST["kon"]);
$stmt->bindParam(':telefon', $_POST["telefon"]);
$stmt->bindParam(':epost', $_POST["epost"]);
$stmt->bindParam(':allergier', $_POST["allergier"]);
$stmt->bindParam(':kategori', $_POST["kategori"]);
$stmt->bindParam(':brodtext', $_POST["brodtext"]);
$stmt->bindParam(':eid', $_POST["eid"]);
$stmt->execute();
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
}
$link = null;
?>
I have tried without the usage of (`), but the messsage is the same.
Upvotes: 1
Views: 25433
Reputation: 13766
This error basically is telling you there's an error in your SQL syntax - the surrounding PHP isn't going to help you, so long as you're troubleshooting the correct query.
(reformatted for easier viewing)
INSERT INTO
`eventcustomers`
(`namn`,
`enamn`,
`datum`,
`personnr`,
`kon`,
`telefon`,
`epost`,
`allergier`,
`kategori`,
`brodtext`,
`reseto`,
`eid`)
VALUES
(:namn,
:enamn,
NOW(),
:personnr,
:kon,
:telefon,
:epost,
:allergier,
:kategori,
:brodtext,
time(), /* this is your problem */
:eid)
Here's your clue: the right syntax to use near '), NULL)'
: You know you've got a close parentheses, comma, value, close parentheses.
That points to your use of the time()
function. You're using it like NOW()
but that's not how it's intended to be used. You need an argument in there.
I believe you just want to use NOW()
instead, if it's a time-only field it'll automatically use only the time portion of the timestamp.
VALUES
(:namn,
:enamn,
NOW(),
:personnr,
:kon,
:telefon,
:epost,
:allergier,
:kategori,
:brodtext,
NOW(), /* fixed! */
:eid)
Upvotes: 1