How to create records in the MYSQL table according to the value of the variable $ nregistros PHP

2

I have a $ nregistry variable that defines the value of records to insert into the "forms" table.

If $ nregisters is equal to 4 for example, it should enter 4 rows as follows:

formularios
id|campo
6 | 1
7 | 2
8 | 3
9 | 4

The field "id" is autoindex, and the field "field" should start at 1 and go to the final value of $ nregistros, in this case, 4

How can I do this?

    
asked by anonymous 02.10.2018 / 21:04

1 answer

2

For this, a simple for can solve.

Try something like:

<?php
    $nregistros = 4;
    for($n = 1; $n <= $nregistros; $n++) {       
        echo "INSERT INTO formularios SET campo = $n; ";
    }
?>

In place of echo would enter your insertion query in the database.

In this way, I got the following output:

INSERT INTO formularios SET campo = 1; INSERT INTO formularios SET campo = 2; INSERT INTO formularios SET campo = 3; INSERT INTO formularios SET campo = 4;
  

Remember that if the number that is in the variable $nregistros   is very long, there will be a loss of performance, due to the fact   of multiple insert .

    
02.10.2018 / 21:37