MySQL 插入错误

发布于 2024-10-27 08:17:23 字数 1077 浏览 2 评论 0原文

好的,当尝试插入数据库时​​,我收到此错误

“您的 SQL 语法有错误; 检查对应的手册 您的 MySQL 服务器版本 在 '@email.com 附近使用正确的语法, UT,84505,现在(),69.169.186.192)' 第 1 行”

我无法找出问题所在。这是我的插入语句的代码。

$insert_query = sprintf("INSERT INTO contacts (first_name, last_name, email, state, zip, date, ip) VALUES (%s, %s, %s, %s, %s, NOW(), %s)",
                        $fname,
                        $lname,
                        $email,
                        $state,
                        $zip,
                        $ip);


$result = mysql_query($insert_query, $connection) or die(mysql_error());

我的表具有以下结构:

    id int(11)                              
    first_name  varchar(100)                                 
    last_name   varchar(100)                             
    email   varchar(100)                                 
    state   varchar(3)                               
    zip int(10)                             
    date    datetime                                
    ip  varchar(255)

Ok, when trying to insert into the database I'm getting this error

"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 '@email.com,
UT, 84505, NOW(), 69.169.186.192)' at
line 1"

I can't figure out the problem. Here is the code for my insert statement.

$insert_query = sprintf("INSERT INTO contacts (first_name, last_name, email, state, zip, date, ip) VALUES (%s, %s, %s, %s, %s, NOW(), %s)",
                        $fname,
                        $lname,
                        $email,
                        $state,
                        $zip,
                        $ip);


$result = mysql_query($insert_query, $connection) or die(mysql_error());

My table has the following structure:

    id int(11)                              
    first_name  varchar(100)                                 
    last_name   varchar(100)                             
    email   varchar(100)                                 
    state   varchar(3)                               
    zip int(10)                             
    date    datetime                                
    ip  varchar(255)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千仐 2024-11-03 08:17:23

您需要在插入语句中引用所有字符串类型列。将 sprintf 格式中的 %s 替换为 '%s'

如果您还没有阅读过 SQL 注入,请阅读相关内容。

You need to quote all the string-type columns in the insert statement. Replace %s with '%s' in the sprintf format.

Please read about SQL Injection if you haven't done so already.

你是暖光i 2024-11-03 08:17:23

这可能会帮助你..

$insert_query = "INSERT INTO contacts set first_name = '$fname', last_name = '$lname', email = '$email', state = '$state', zip = '$zip', date = ". time() .", ip = '$ip')";


$result = mysql_query($insert_query, $connection) or die(mysql_error());

如果你想检查查询

echo $insert_query;

This may help you..

$insert_query = "INSERT INTO contacts set first_name = '$fname', last_name = '$lname', email = '$email', state = '$state', zip = '$zip', date = ". time() .", ip = '$ip')";


$result = mysql_query($insert_query, $connection) or die(mysql_error());

if you want to check query

echo $insert_query;
栀子花开つ 2024-11-03 08:17:23

如果您可以回显 $insert_query,将会有所帮助,但看起来您没有在 varchar 参数周围加上引号。

$insert_query = sprintf("INSERT INTO contacts (first_name, last_name, email, state, zip, date, ip) VALUES ('%s', '%s', '%s', '%s', '%s', NOW(), '%s')",
                        $fname,
                        $lname,
                        $email,
                        $state,
                        $zip,
                        $ip);

顺便说一句,您的插入中有一个额外的列 - 现在似乎与列无关。
顺便说一句,我假设 ZIP 是一个 varchar 列,而不是数字。

It would help if you could echo out the $insert_query, but it looks like you're not putting quotes around the parameters that are varchars.

$insert_query = sprintf("INSERT INTO contacts (first_name, last_name, email, state, zip, date, ip) VALUES ('%s', '%s', '%s', '%s', '%s', NOW(), '%s')",
                        $fname,
                        $lname,
                        $email,
                        $state,
                        $zip,
                        $ip);

By the way, you have an extra column in your insert - NOW doesn't appear related to a column.
I'm assuming ZIP is a varchar column, not a number, by the way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文