views:

45

answers:

3

Code:

for($i = 1; $i <= $arr['var']; $i++) {

  if($i == $arr['var']):

    $insert .= '('.$n_id.', 12)';

  else:

    $insert .= '('.$n_id.', 12),';

  endif;

}

$uz = mysql_query("INSERT INTO `cars`
                   (n_id, auto_id)
                   VALUES
                   '$insert'") or die(mysql_error());

Why it gives me a SQL syntax error? What am I doing wrong?

+2  A: 

You're missing string concatentation in the INSERT statement:

$uz = mysql_query("INSERT INTO `cars`
                     (n_id, auto_id)
                   VALUES "
                    . $insert .") or die(mysql_error());

That's assuming you aren't also having an issue with the trailing comma for tuple support.

OMG Ponies
Absolutely right, thank you very much.
hey
Why is your answer community wiki?
@MrXexxed: My choice, cuz I'm not 100% on my PHP
OMG Ponies
A: 

Using backticks (around cars) is incorrect when mixed with single quotes (around $insert), to the best of my knowledge, in mySQL. Pick one or the other - single quotes are probably recommended for standardization.

Ivan
A: 

On General SQL Management it gives this example for an insert into:

INSERT INTO [table]
( [field1], [field2], [field3] ) VALUES
( '[value1.1]', '[value1.2]', '[value1.3]' ),
( '[value2.1]', '[value2.2]', '[value2.3]' ),
( '[value3.1]', '[value3.2]', '[value3.3]' ),
etc.

You might have some syntax problems. I recommend printing out

"INSERT INTO `cars`
       (n_id, auto_id)
       VALUES
       '$insert'"

to see what you are sending.

Kyra