简体   繁体   中英

Having trouble with SQL query

I'm using this query (I changed it):

// SQL query
$squl = "INSERT INTO 'messages' ('id','name' ,'email' ,'subject' ,'content','userid') VALUES ( null,'".$name."',  '".$mail."',  '".$subject."',  '".$content."','');"; 

// mysql query
$query = mysql_query($squl) or die("message query problem: ".  mysql_error()); 

I get 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 ''messages' ('id','name' ,'email' ,'subject' ,'content','userid' )VALUES ( null,'' at line 1

What is causing this?

.``) You used a period here instead of a comma so the function is only receiving 5 columns when it needs 6.

Update:

As the commenter below points out, you've replaced the backticks with quotation marks.

$squl="INSERT INTO `messages` (`id`,`name` ,`email` ,`subject` ,`content`,`userid` )VALUES ( null,'$name',  '$mail',  '$subject',  '$content','');";
(id,name ,email ,subject ,content,userid )

( NULL,".$name.", ".$mail.", ".$subject.", ".$content."**.**``);

you are using '.' instead of ,

Well, that's about the clearest message you get from SQL. You try to insert 5 values into 6 columns.

The problem that there's no comma between the last two values. Instead there's a . which makes the parser think it's only one value.

You are trying to insert into 6 columns:

  1. id
  2. name
  3. email
  4. subject
  5. content
  6. userid

But have only specified 5 values:

  1. NULL
  2. $name
  3. $mail
  4. $subject
  5. $content

You've got a dot where you should have a comma:

".$subject."`,  `".$content."`.``);";

Change that last dot to a comma and you should be golden

You've got 6 fields in your fields list, but are inserting only 5 values in your values list. Looks like you've got a . instead of a , :

`,  `".$subject."`,  `".$content."`.``
                                   ^--- here

As well, there is NO reason to use repeated string concatenation as you are. PHP can insert variables into double-quoted strings quiet easily:

$sql = "INSERT INTO .... (...) VALUES (NULL, '$name', '$mail', '$subject', '$content', '')";

Note that the 'null' value is not quoted. Backticks are there to escape reserved words. If you intend to insert a real database null, then use the bare word null . If you want a literal string 'null' to go in, then quote it normally: 'null' .

You have six fields listed the first set of parentheses and only five fields in VALUES. That's what column count means.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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