PHP query with response from another query

Asked

Viewed 35 times

0

I run a query that returns me a list of Ids. With each ID of this list I want to do an Insert in a second table.

My code:

<?php

$db = mysqli_connect("localhost","root","","test") or die("Erro ao conectar na base de dados MySQL!");
$query = "select id from country";
$result = mysqli_query($db, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $var = $row["id"];
    mysqli_query($db, "insert into testando (numero) values ('$var')");

} 

mysqli_close($db);

?>

When executed, only 1 record is inserted into the database. If I run a second time, nothing happens. Where I am missing??

  • 2

    The error may be being caused by your database structure, for example, if number is some pk, it shows neither an error on the screen?

  • No...no screen errors. And "number" is not a PK in the table "testing".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.