SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误 [Php PDO]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42633343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:39:52  来源:igfitidea点击:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax [Php PDO]

phpmysqlpdo

提问by Dentalbudet

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

错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以获取在第 2 行附近的 '), NULL)' 附近使用的正确语法

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.

我试过不使用 (`),但消息是一样的。

回答by Jason

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.

这个错误基本上是在告诉您 SQL 语法中存在错误 - 只要您对正确的查询进行故障排除,周围的 PHP 不会帮助您。

(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.

这是你的线索:the right syntax to use near '), NULL)': 你知​​道你有一个右括号、逗号、值、右括号。

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.

这表明您使用了该time()功能。您正在使用它,NOW()但这不是它的用途。你需要在那里争论。

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.

我相信你只是想NOW()改用,如果它是一个仅限时间的字段,它会自动只使用时间戳的时间部分。

VALUES
    (:namn,
    :enamn,
    NOW(),
    :personnr,
    :kon,
    :telefon,
    :epost,
    :allergier,
    :kategori,
    :brodtext,
    NOW(), /* fixed! */
    :eid)