How to do this insertion dynamically?

-2
  

How to do this insertion dynamically knowing the total amount of columns?

mysql_query("INSERT INTO VOT_PART_MUN_ZONA VALUES ( 

   '$dados[0]',
   '$dados[1]',
   '$dados[2]',
   '$dados[4]',
   '$dados[...]',
   '$dados[19]'

)");
    
asked by anonymous 23.02.2015 / 19:24

1 answer

0

Although not a best practice, you can do as follows.

Suppose your array has 500 data, such as the ones generated below by the range function.

We could use the function implode to be able to bring all the values.

$dados = range(1, 500);


$insert = sprintf('INSERT INTO tabela VALUES("%s")', implode('","', $dados));


echo $insert;

link

The recommendation would be to inform you the fields where you will enter this data. One of the benefits that can be cited is the easy reading of the code, whoever it is that is the edit.

    
23.02.2015 / 19:39