MySQL 插入错误
好的,当尝试插入数据库时,我收到此错误
“您的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在插入语句中引用所有字符串类型列。将 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.
这可能会帮助你..
如果你想检查查询
This may help you..
if you want to check query
如果您可以回显 $insert_query,将会有所帮助,但看起来您没有在 varchar 参数周围加上引号。
顺便说一句,您的插入中有一个额外的列 - 现在似乎与列无关。
顺便说一句,我假设 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.
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.