What's wrong with this MySQL query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

What's wrong with this MySQL query?

Post by pilau »

I'm making a trivia game project, and now am working on the Admin CP.
The part I'm on now is the part where the administrator adds a question to a table on a DB:

Code: Select all

mysql_query("INSERT INTO questions (author, category, query, answer_a, answer_b, answer_c, correct) VALUES ($_POST[\'author\'], $_POST[\'category\'], $_POST[\'query\'], $_POST[\'answer_a\'], $_POST[\'answer_b\'], $_POST[\'answer_c\'], $_POST[\'correct\']");
PHP generates the following error:
Parse error: syntax error, unexpected T_BAD_CHARACTER, expecting T_STRING or T_VARIABLE or T_NUM_STRING in [filename]

Thanks.
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post by tores »

Put brackets around the variables. And data inserted to mysql should be single-quoted

Code: Select all

mysql_query("INSERT INTO questions (author, category, query, answer_a, answer_b, answer_c, correct) VALUES ('{$_POST[\'author\']}', '{$_POST[\'category\']}', '{$_POST[\'query\']}', '{$_POST[\'answer_a\']}', '{$_POST[\'answer_b\']}', '{$_POST[\'answer_c\']}', '{$_POST[\'correct\']}'");
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Thanks.
Why do I need to put brackets around the variables?
And in what other cases do I need to do it? (if there are ases like that)
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Your query gave an error.
I changed it to:

Code: Select all

<?php

mysql_query("
 INSERT INTO questions
  (author, category, query, answer_a, answer_b, answer_c, correct)
 VALUES (
  {$_POST['author']},
  {$_POST['category']},
  {$_POST['query']},
  {$_POST['answer_a']},
  {$_POST['answer_b']},
  {$_POST['answer_c']},
  {$_POST['correct']}

");

?>
And it didn't give an error back.
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post by tores »

You need brackets when you use a string or variable to access a array-cell or object-property.

Code: Select all

echo "{$arr['something']}";
echo "{$obj->$property}";
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Oh alright got it.
So that goes for SQL queries as well.
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post by tores »

It goes for everything between double-quotes (and also backticks I think)
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Backticks? What're backticks?
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Post by tores »

pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Alright thank you.
Post Reply