为什么我的MySQL查询失败

时间:2012-02-27 23:23:21

标签: php mysql database

我无法让它工作,不断收到错误消息。

错误

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 '-mail, password, 
birth_date, age, sex, profile_text, zip_code, zip_code_state, c' at line 1

代码

mysql_query("INSERT INTO users (username, e-mail, password, birth_date, age, sex,
      profile_text, zip_code, zip_code_state, coins, rank, profile_visits, 
      profile_likes, profile_image, profile_points, activated, deleted, reg_time, 
      last_active_time, reg_ip)
    VALUES ('$randomName', 'awduhawd@hotmail.com', 'awd', '21/05/1990','0','2',
      '0','4306','Sandnes','0','user','0','0','$image','0','0','0','$time',
      '$time','0')")
 or die(mysql_error());

4 个答案:

答案 0 :(得分:9)

用反引号围绕e-mail ......

`e-mail`,

除此之外,你不能放弃-

答案 1 :(得分:2)

- 符号是SQL中的保留符号,需要在反引号中包装电子邮件,即“电子邮件”。

答案 2 :(得分:1)

经验法则:反引号中的列名并连接字符串变量以便于阅读,MySQL日期格式为Y-m-d(1990-05-21)

mysql_query("INSERT INTO users (`username`, `e-mail`, `password`, `birth_date`, `age`,`sex`,       
  `profile_text`, `zip_code`, `zip_code_state`, `coins`, `rank`, `profile_visits`, 
  `profile_likes`, `profile_image`, `profile_points`, `activated`, `deleted`, `reg_time`, 
  `last_active_time`, `reg_ip`)
VALUES ('".$randomName."', 'awduhawd@hotmail.com', 'awd', '1990-05-21','0','2',
  '0','4306','Sandnes','0','user','0','0','".$image."','0','0','0','".$time."',
  '".$time."','0')")
 or die(mysql_error());

答案 3 :(得分:0)

如果你使用php,不要在变量周围使用单引号,它们将不会被解析。

'$randomName' = wrong
either use "$randomName"
or use "'.$randomName.'"