简体   繁体   English

我无法弄清的mysql错误

[英]Mysql mistake that I cant figure out

I am trying to insert data to my table: 我正在尝试向表中插入数据:

 $userName="NewOrder";//use cookie to get name;
     $selectCurrentUser=" SELECT user_id FROM users WHERE user_name='$userName'; ";




  mysql_query(" INSERT INTO threads (topic ,date ,views ,user_id ,responses ,closed ,title ,close_voted ,content)
          VALUES('$threadTopic', NOW(), 0, '$selectCurrentUser', 0, 'false', '$threadTitle', 0, '$threadContent');") or die(mysql_error());

      mysql_close();

There is a mistake in my second statement. 我的第二个陈述有误。 It says: 它说:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NewOrder'; 查看与您的MySQL服务器版本相对应的手册,以在“ NewOrder”附近使用正确的语法; ', 0, 'false', '11111111111111111111111', 0, ' 22222222222222222222' at line 2 ',0,'false','11111111111111111111111',0,'22222222222222222222'在第2行

My table structure: 我的表结构:

פעולה thread_id int(11) None AUTO_INCREMENT threadולהthread_id int(11)无AUTO_INCREMENT
topic varchar(50) 主题varchar(50)
date date None 日期日期无
views int(11) None views int(11)无
user_id int(11) None user_id int(11)无
responses int(11) None 响应int(11)无
closed varchar(10) utf8_unicode_ci None 已关闭varchar(10)utf8_unicode_ci无
title varchar(100) utf8_unicode_ci None 标题varchar(100)utf8_unicode_ci无
close_voted int(11) None close_voted int(11)无
content text utf8_unicode_ci None 内容文本utf8_unicode_ci无

Instead of '$selectCurrentUser' , use ($selectCurrentUser) . 使用($selectCurrentUser)代替'$selectCurrentUser' Also, I don't think you should put a semicolon at the end of your first SQL statement. 另外,我不认为您应该在第一个SQL语句的末尾添加分号。

EDIT - for clarity I mean that this statement 编辑 -为了清楚起见,我的意思是说

  mysql_query(" INSERT INTO threads (topic ,date ,views ,user_id ,responses ,closed ,title ,close_voted ,content)
      VALUES('$threadTopic', NOW(), 0, '$selectCurrentUser', 0, 'false', '$threadTitle', 0, '$threadContent');") or die(mysql_error());

should be written as follows (with single-quotes around $selectCurrentUser replaced with parentheses): 应该这样写(在$ selectCurrentUser周围用单引号替换括号):

  mysql_query(" INSERT INTO threads (topic ,date ,views ,user_id ,responses ,closed ,title ,close_voted ,content)
      VALUES('$threadTopic', NOW(), 0, ($selectCurrentUser), 0, 'false', '$threadTitle', 0, '$threadContent');") or die(mysql_error());

Additionally, the following statement 此外,以下语句

$selectCurrentUser=" SELECT user_id FROM users WHERE user_name='$userName'; ";

should have the first semicolon removed, like so: 应该删除第一个分号,如下所示:

$selectCurrentUser=" SELECT user_id FROM users WHERE user_name='$userName' ";

Looks from the error message that it is not taking the quotes the way you are expecting. 从错误消息中可以看出,它没有按照预期的方式使用引号。

 'NewOrder'; ', 0, 'false', '11111111111111111111111', 0, ' 22222222222222222222' at line 2

  $userName="NewOrder";//use cookie to get name;
  $selectCurrentUser=" SELECT user_id FROM users WHERE user_name='$userName'; ";
   mysql_query(" INSERT INTO threads (topic ,date ,views ,user_id ,responses ,closed ,title ,close_voted ,content)
      VALUES('$threadTopic', NOW(), 0, '$selectCurrentUser', 0, 'false', '$threadTitle', 0, '$threadContent');") or die(mysql_error());

  mysql_close();

You only specify NewOrder as the first statement and put single quotes aruond it - I dont think you need to specify the semicolon at the end of the first SQL. 您仅将NewOrder指定为第一条语句,并在其前后加上单引号-我认为您无需在第一条SQL的末尾指定分号。

The error implies it is not seeing the code from the end quote of $userName to the 0 after $selectCurrentUser 该错误表明在$ selectCurrentUser之后没有看到从$ userName的结束引号到0的代码。

You could add additional quotes and concatenate with . 您可以添加其他引号并与串联。 like this 像这样

  $selectCurrentUser=" SELECT user_id FROM users WHERE user_name='".$userName."'"; 

and similarly for the second statement. 和第二个陈述类似。 Hope that helps. 希望能有所帮助。

A couple of things here: 这里有几件事:

  1. I don't see a field in your table structure called user_name . 我在您的表结构中看不到名为user_name的字段。 Is that a typo, or might you be looking for data that isn't there? 那是拼写错误,还是您正在寻找不存在的数据?
  2. Secondly, you shouldn't use a delimiter in your first statement and it should be surrounded by brackets like this: 其次,您不应在第一个语句中使用定界符,而应将其用方括号括起来,如下所示:

     $selectCurrentUser="(SELECT user_id FROM users WHERE user_name='$userName' LIMIT 1)"; 
  3. If that still fails then I would advise retrieving user_id first (using PHP) and then explicitly passing that to the second query. 如果仍然失败,则建议先检索user_id (使用PHP),然后将其显式传递给第二个查询。 I would do it like this: 我会这样做:

     $userName="NewOrder";//use cookie to get name; $selectCurrentUser = 0; // retrieve the user_id first $result = mysql_query("SELECT user_id FROM users WHERE user_name='$userName' LIMIT 1"); while($row = mysql_fetch_assoc($result)){ // set $selectCurrentUser to the only id retrieved $selectCurrentUser = $row['user_id']; } // pass the exact user_id to the second query mysql_query(" INSERT INTO threads (topic ,date ,views ,user_id ,responses ,closed ,title ,close_voted ,content) VALUES('$threadTopic', NOW(), 0, $selectCurrentUser, 0, 'false', '$threadTitle', 0, '$threadContent')") or die(mysql_error()); mysql_close(); 

    Note: I have removed the quotes from around $selectCurrentUser in the second query. 注意:在第二个查询中,我已删除$selectCurrentUser附近的引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM