Insert multiple records into Mysql based on an X value and add fixed values

Asked

Viewed 74 times

0

I need to enter multiple records in the database, but without coming from a form. For example:

I have a 50 value (it would be the number of records I need to enter) and a mooring id that needs to be the same for all records.

My question is: how do I insert without having to come from a form these values?

I’ve tried with the examples here on the forum, but they all use values passed by forms.

In my case I have no form I have only the amount of records I need to enter.

  • It is only set the values can play inside a for or mount a single inset.

  • I tried, but I couldn’t.

  • @Rray can give me a light ?

  • There are some examples of how to mount the Insert or pick up the inserted id. https://answall.com/q/89841/91 e https://answall.com/q/94216/91

  • In the examples you don’t have the logic I need. As in the example I gave: I need to insert 50 new records, ID Primary is AUTO, The name would have to be NAME 1, NAME 2, NAME 3 so on and ID binding with other tables would be a fixed for example 2. 1, NAME 1, 2 1, NAME 2, 2

  • @Rray understood how it would be ?

Show 1 more comment

1 answer

0

I got a solution! I don’t know if it’s the right solution to what I wanted, but it’s the one I found.

I’ll leave it here for whoever’s looking for something similar:

//Aqui eu defino a quantidade de registros que eu quero inserir. No caso aqui são 109 registros.
$valores = range(1, 109);

//Aqui posso definir o valor que eu preciso que seja igual em todos os registros adicionados
$id_empresa = '2';
$nome_pesquisa = 'Pesquisa Setor A';

for ($i = 0, $total = count($valores); $i < $total; $i++) {
    $conn->query("INSERT INTO pesquisa(id_empresa, nome) VALUES('$id_empresa','$nome_pesquisa')");
}

Browser other questions tagged

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