2013-08-03 60 views
-2

Thankyou提前回答了这个问题,这是我的代码的一部分,它不起作用。SQL语法错误,找不到原因

$sql_insert = "INSERT INTO 'users' 
(
`ragionesociale`, 
`user_name`, 
`user_email`, 
`pwd`, 
`nome`, 
`cognome`, 
`tel`, 
`tel2`, 
`website`, 
`date`, 
`users_ip`, 
`activation_code`, 
`piva`, 
`cf`, 
`azienda`) 

VALUES 

(
'$data[ragionesociale]', 
'$data[user_name]', 
      '$user_email', 
      '$sha1pass', 
'$data[nome]', 
'$data[cognome]', 
'$data[tel]', 
'$data[tel2]', 


'$data[website]' , 
now(), 
'$user_ip', 
'$activ_code', 

$data[piva],$data[cf], 
$data[azienda])";mysql_query($sql_insert) or die("Insertion Failed:<br>$sql_insert<br>" . mysql_error()); 

,这是给定的结果:

INSERT INTO 'users' ( 
    `ragionesociale`, 
    `user_name`, 
    `user_email`, 
    `pwd`, 
    `nome`, 
    `cognome`, 
    `tel`, 
    `tel2`, 
    `website`, 
    `date`, 
    `users_ip`, 
    `activation_code`, 
    `piva`, 
    `cf`, 
    `azienda` 
) 
VALUES ( 
    'La Fabbrica dei Giochi', 
    'uuuuuuu', 
    '[email protected]', 
    'e258c8aac30eba143e1f3sssss6dc1cdc4d58ff1095', 
    'pietro', 
    'rossi', 
    '234234234', 
    '345.4346865', 
    'www.ssss.it' , 
    now(), 
    '212.124.171.131', 
    '9962', 
    2323232, 
    tsssss6i819s, 
    1 ) 

错误消息:

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 ''users' 
(`ragionesociale`, `user_name`, `user_email`, `pwd`, `nome`, `cogno' at line 1 

我找不到什么是错在此代码,我在浪费2小时... 。

+3

在'users'上使用反引号而不是单引号,就像您为列操作一样。 – vee

回答

1

这是一个SQL字符串文字:

'users' 

这是一个表名:

users 

这是一个(MySQL的)报价表名:

`users` 

SQL标准希望"users"和各种微软的数据库会想[users]

你不能插入字符串,你甚至不能考虑它。你的SQL应该看起来更像是这样的:

$sql_insert = "INSERT INTO `users` ... 
2

与反引号替换单引号:

$sql_insert = "INSERT INTO `users` 

这应该解决这个问题。

尽可能地使用反引号。它可以帮助您防止保留关键字之间的冲突(例如password)。

重要提示:您目前的表单查询容易受到SQL注入的影响。考虑使用mysql_real_escape_string()或甚至更好的转义使用PDOmysqli转义输入。

0

尝试用

 `user` 

替换 '用户'。 MySql无法识别通常的

0

我会将'用户'更改为users加上第二个最后一个字段$data[cf]传递字母数字(tsssss6i819s),但只分配数字。将其更改为,'$data[cf]'。也许检查字段aziendapiva以及。