Send data to the database according to the select tag via PHP [closed]

1

I am making a form to send some data to the bank, to expedite a process, my question is the following, I have 5 systems that I need to update, so I made a select in html. Home I need to send each option of select to a different bank table, for example, if I select "System 1" it goes to table "System 1" in the database, if it is "System 2" in the "System 2" table and so on, in each table the same fields would only change the name, list it on another web site screen. Home What would be the simplest way for me to do this in PHP?

<divclass="form-group">
        <form action="envia.php" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label class="col-sm-3 control-label"><b>Sistema</b></label>    
                <div class="col-md-10">
                    <select name="sistema" class="form-control" required/>
                        <option>Escolha o sistema que deseja importar</option>
                        <option value="sis1">Sistema 1</option>
                        <option value="sis2">Sistema 2</option>
                        <option value="sis3">Sistema 3</option>
                        <option value="sis4">Sistema 4</option>
                        <option value="sis5">Sistema 5</option>
                        <option value="sis6">Sistema 6</option>
                        <option value="sis7">Sistema 7</option>
                    </select>
                </div>
            </div>
    
asked by anonymous 06.11.2018 / 14:17

3 answers

3

Considering that you are doing POST and that <select> has the following syntax:

<select name="sistema">
    <option selected>Selecione uma opção* </option>
    <option value="sistema1" > Sistema 1 </option>
    <option value="sistema2" > Sistema 2 </option>
    <option value="sistema3" > Sistema 3 </option>
    <option value="sistema4" > Sistema 4 </option>
    <option value="sistema5" > Sistema 5 </option>
</select>

The balcony is to insert the name of the tables in the value of the options and capture it in PHP .

In your PHP would look like this:

$table = addslashes($_POST['sistema']); //nome da tabela

And at the time of setting up your query , it would look something like:

$query = "INSERT INTO $table (campo1, campo2, campo3) VALUES (valor1, valor2, valor3)";

If you do not want to pass the name of the tables directly to options , pass generic names and treat PHP .

Example:

<select name="sistema">
        <option selected>Selecione uma opção* </option>
        <option value="table1" > Sistema 1 </option>
        <option value="table2" > Sistema 2 </option>
        <option value="table3" > Sistema 3 </option>
        <option value="table4" > Sistema 4 </option>
        <option value="table5" > Sistema 5 </option>
    </select>

No PHP :

$table = addslashes($_POST['sistema']);

switch($table) {
    case 'table1':
        $table = 'sistema1'; //definindo o valor da table como sistema1 (nome da tabela)
    break;
    case 'table2':
        $table = 'sistema2'; //definindo o valor da table como sistema2 (nome da tabela)
    break;
    case 'table3':
        $table = 'sistema3'; //definindo o valor da table como sistema3 (nome da tabela)
    break;
    case 'table4':
        $table = 'sistema4'; //definindo o valor da table como sistema4 (nome da tabela)
    break;
    case 'table5':
        $table = 'sistema5'; //definindo o valor da table como sistema5 (nome da tabela)
    break;
}

$query = "INSERT INTO $table (campo1, campo2, campo3) VALUES (valor1, valor2, valor3)";

As you did not inform your structure, I made a more generic example possible.

    
06.11.2018 / 14:37
1
<?php

$select = $_POST['seu_select'];

switch ($select) {
    case 0:
    $sql = "INSERT INTO tabela1(campo1,campo2,campo3) VALUES ('valor1', 'valor2', 'valor3')";
    mysqli_query($conexao,$sql);

    break;

    case 1:
    $sql = "INSERT INTO tabela2(campo1,campo2,campo3) VALUES ('valor1', 'valor2', 'valor3')";
    mysqli_query($conexao,$sql);
    break;

    case 2:
    $sql = "INSERT INTO tabela3(campo1,campo2,campo3) VALUES ('valor1', 'valor2', 'valor3')";
    mysqli_query($conexao,$sql);
    break;
}

?>
    
06.11.2018 / 14:24
0

If the name of your <select> is table do so:

$sql="INSERT into '".$_POST[tabela]."' set campo1='".$_POST[var1]."'";
mysqli_query($con,$sql);

obs: this is if you are using mysqli if it is mysql only

$sql="INSERT into '".$_POST[tabela]."' set campo1='".$_POST[var1]."'";
mysql_query($sql);

where campo1 is an ex you can enter the field names of your table and names as variable, and the method of your form must be post

    
06.11.2018 / 14:22