Multiple insert with PHP to MySQL not working for me but working for other people

I can see that this question gets asked a lot and I've tried following some answers that people said that worked for them but still it is not working for me. I want to insert into my DB a variable number of elements. Here is my code:

$actual_id = $db->insert_id;

$sql = array(); 
foreach( $relacionado as $row ) {
    $sql[] = '('.$actual_id.', '.$row.')';
$query2 = 'INSERT INTO relaciones (id, relacionadocon) VALUES '.implode(', ', $sql);

echo $query2;
$result2 = $db->query($query2);

    echo "<p>".$db->affected_rows."fields inserted</p>";
    echo "<p>Error</p>";

The thing is it works when I give it only one element but returns an error when there are more elements. I tried adding ", " instead of "," between the elements but nothing happens... May it be because of the configuration of MySQL? Because I can't think on anything else as it seems to work for other people... THanks!

EDIT: This is the result for echo $query2: INSERT INTO relaciones (id, relacionadocon) VALUES (65, 12), (65, 26) EDIT2: I created the table using phpmyadmin. There are 2 tables. The first one registers information and uses an autoincreasing ID. So, the colums are ID, Title and Comment. But every comment is related to 1 or more comments. That is why there is another table that has 2 colums: ID and RelatedTo. When, for example, comment number 5 is created, you can relate it with comments 1, 2 and 3. That is why the INSERT should pass: (5,1), (5,2), (5,3). Once again, if I only had (5,1), it would work. I didn't give a primary key to the ID of the second table, only to the first.


I'm thinking the ID field is the primary key and shouldn't be written to. Can you try and run INSERT INTO relaciones (id, relacionadocon) VALUES (65, 12), (65, 26) within phpmyadmin to see what the result is?

Need Your Help

Open JSP in new browsertab from serverside code

jsp jsf-2 primefaces

Is it possible to open a resource into a new browser Tab (like target="_newtab" for command buttons) from server side jsf code?

Migrating to self hosted Parse Server isn't giving me the logged user


I'm trying to migrate my Parse server to my own server instance in DigitalOcean. After deploying my parse-server I'm falling in some issue I can't understand.