views:

55

answers:

3
+2  Q: 

Query doesn't work

I get the following error message when I try this query:

$query .= "($tid, {$_POST['type']['$i']}, 'Y', NOW())";

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

(18, , 'Y', NOW())' at line 1

Everything in my code works except this line. I know it has to do with $_POST array variable, but i don't know the correct syntax.

If you need more code, I will be happy to include it.

+2  A: 

If you want better, help, show the code that's sending the post variables (probably a form or AJAX request).

But your problem may lie here:

$query .= "($tid, {$_POST['type']['$i']}, 'Y', NOW())";

The post would evaluate to something like $_POST['type']['1']. You probably want $_POST['type'][1].

So try this:

$query .= "($tid, {$_POST['type'][$i]}, 'Y', NOW())";

Jamie Wong
+3  A: 

Your use of single quotes around $i is wrong:

$query .= "($tid, {$_POST['type']['$i']}, 'Y', NOW())";

This will cause $i to be taken literally instead of the value of $i being used (which is what I assume you need).

This should work:

$query .= "($tid, {$_POST['type'][$i]}, 'Y', NOW())";
Jon Cram
Just what I needed. I tried almost every variable of single quotes and braces except for that one. Thanks.
aliov
A: 

As already mentioned. $query .= "($tid, {$_POST['type']['$i']}, 'Y', NOW())";

Should read

$query .= "($tid, {$_POST['type'][$i]}, 'Y', NOW())";

Curious why you have "{ }" in use.

That said, you should always avoid typing variables from $_GET or $_POST straight into code to be used in queries, or any other aspects of your site.

You really should get into the habit or passing the variable via something you have pre-determined.

Not to mention, will you be escaping these variables prior to running the sql query.

Take a look at

http://php.net/mysql_real_escape_string http://php.net/pdo.quote

If you have the time, investigate pdo.

thefreshteapot