l3gion
l3gion

Reputation: 73

Problem Inserting Multiple Arrays into Database

I was inserting multiple arrays into my database using this code:

$queries_cols = array(); 

for($i=0; $i<count($cols); $i++) 
{ 
    $queries_cols [] = "('".$cols[$i]."','".$int_manha1_1[$i]."','".$int_manha1_2[$i]."','" .$int_tarde2_1[$i]."','".$int_tarde2_2[$i]."','".$h1_1[$i].$h1_2[$i]."','".$h2_1[$i].$h2_2[$i]."')" ; 
}

$query_col = "INSERT into cols_ponto_diaria values '".implode(",", $queries_cols)."'";

$result = mysql_query($query_col) or die("Erro: ".mysql_error());

And when I submit the form it output an error:

Erro: 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 ''('Ricardo','08:30:00','12:15:00','14:45:00','18:00:00','0115','1215')'' at line 1

Can anyone find where is the error? I've tried a few solutions but without any success.

Thanks in advance.

Upvotes: 0

Views: 104

Answers (3)

Alexandros V
Alexandros V

Reputation: 560

You could easily find it by printing $query_col as soon as you have it ready.

Anyway, the error I believe is in the implode() call. You don't need the extra quotes.

So instead of

$query_col = "INSERT into cols_ponto_diaria values '".implode(",", $queries_cols)."'";

try

$query_col = "INSERT into cols_ponto_diaria values ".implode(",", $queries_cols);

Upvotes: 1

l3gion
l3gion

Reputation: 73

Solution found. I tried several things and I forgot to try this approach. :(

Sorry for disturbing.

Here's the correct syntax:

$query_col = "INSERT into cols_ponto_diaria values ".implode(",", $queries_cols);

regards

Upvotes: 0

Remove the single quote at the end of this expression,

INSERT into cols_ponto_diaria values 

Don't append the single quote at the end of the expression.

."'"

Upvotes: 1

Related Questions