Insert into multiple input fields in different tables [closed]

7

I have the fields of date, CNPJ, latitude, longitude, site, facebook, on the same screen, only they are 3 different tables, how do I give insert from them from the same button ?? I use to save information but not all fields are required.

    
asked by anonymous 20.09.2016 / 14:05

2 answers

0

I understand that all fields are in a single FORM, and that you send them all in the same request for processing / writing in different tables. You can separate the data passed by the form and create independent SQL queries to write them. Assuming script receives data via $_POST :

´foreach($_POST as $var => $value){
  if($var == 'cnpj'){
   $query = "INSERT INTO table_name1($var) VALUES ($value); ";
  }
  if($var == 'latitude'){
   $query = $query . "INSERT INTO table_name2($var) VALUES ($value); ";
  }}
  echo $query;´

The answer should be this one here:

´INSERT INTO table_name1(cnpj) VALUES (000000000000); 
 INSERT INTO table_name2(latitude) VALUES (1245165421341);´

At the end, just run the $query formed.

    
20.09.2016 / 15:09
0

Hello, how are you?

You can use input arrays in the html by separating for each table you want to enter the data, for example:

    <form action="inputArray.php" method="post">

        <label>tbl1 Nome</label>                
        <input type="text" name="tbl1[nome]"/><br/>
        <label>tbl1 Sobrenome</label>
        <input type="text" name="tbl1[sobrenome]"/><br/>
        <label>tbl1 Idade</label>
        <input type="text" name="tbl1[idade]"/><br/><br/>

        <label>tbl2 Nome</label>
        <input type="text" name="tbl2[nome]"/><br/>
        <label>tbl2 Sobrenome</label>
        <input type="text" name="tbl2[sobrenome]"/><br/>
        <label>tbl2 Idade</label>
        <input type="text" name="tbl2[idade]"/><br/>
        <input type="submit" value="Enviar"/>
    </form>

If on the landing page you give print_r($_POST) , it will have the following result:

Array ( 
[tbl1] => Array ( [nome] => [sobrenome] => [idade] => )
[tbl2] => Array ( [nome] => [sobrenome] => [idade] => ) 
)

where tbl1 and tbl2 are your tables, after that it is only the inserts

$tbl1 = $_POST['tbl1'];
$sql = "INSERT INTO tbl1 (nome, sobrenome, idade) VALUES($tbl1['nome'], $tbl1['sobrenome'], $tbl1['idade'])";'

and then just repeat for the other tables.

I hope I have helped. I am available for any questions. Thanks.

    
20.09.2016 / 18:56