Send data to Database according to <select> tag via PHP

Asked

Viewed 770 times

1

I’m 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 html.

I need to send each option of select for a different bank table, for example, if I select the "System 1" he goes to the table "System 1" on the bench, if it is "System 2" on the table "System 2" and so on, in each table would have the same fields only would change the name, then I list it on another screen of the site.

What would be the simplest way for me to do this in PHP ?

Print do formulário

<div class="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>
  • Leave the name of the dynamic table in the query. Then it will insert in the table chosen by select

  • Can you give me an example of how to do this ? Just so I have a search base

3 answers

3


Taking into account that you are performing a POST and that the <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 drawback is to enter the name of the tables in the value of the options and capture it in the PHP.

In his PHP would look like this:

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

And when it’s time to ride your query, would look something like:

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

If you do not want to pass the table name directly to us options, pass generic names and treat on 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>

In the 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)";

Since you didn’t inform your structure, I made a more generic example possible.

  • I edited my question and put the structure, but that’s basically what you commented, as I’m using the action in another file, I do all the php part in my send.php file, right ? Any questions, I comment again.

  • @lucasecorrea exactly, all part of the php will be made inside the file you are sending the data.

  • 1

    @lucasecorrea edited and added a new example, take a look at

  • It helped me a lot to explain, just take one more question, I will need to pass 2 fields of the form to the bank (http://prntscr.com/lf2zlb) that are with these name of the print, there in php I need to create two variables to pass record the information in the bank? (http://prntscr.com/lf3111). Take a look at the prints and I made myself if I’m on the right track.

  • That’s right, you will capture all fields sent via POST, the way you did is correct.

  • just one more silly question, I’m using phpmyadmin, if I create a field from a DATE type bank table the default will be yyyy-mm-dd there in phpmyadmin, if I type in my form in the dd-mm-yyyy pattern, there’s some way to convert to fall correct into the bank ?

  • Just receive the data and treat before sending to the bank. $date = addslashes(date('Y-m-d', strtotime(str_replace('/', '-', $_POST['date'])))); thus makes the treatment and replaces all "" by "-" to save in the bank.

  • Dude, I’m trying to make it explode, implode, and the way you told me to, but you’re making a mistake. This is the print of how PHP looks (http://prntscr.com/lf5jab). Where I’m missing ?

  • (http://prntscr.com/lf5lbf) the seat is so mounted

  • What’s the bug?? From a var_dump($_POST['data']); and then a var_dump($data) and see how it’s coming. Remember that the use of str_replace is applied only if your date is coming in this front format: '01/01/2018

  • (http://prntscr.com/lf62i4) Var_dump brought this, in the form you said, (http://prntscr.com/lf631i) has a mask for 00/00/9999. I tried several treat this date to save correctly in the bank but could not.

  • The error is that the date variable has not been declared. Revise lines 144 and 170. Note: take the $data = $_POST['data'] because you already have this data in the next line.

  • (http://prntscr.com/lf69qo) put what you had sent me before and didn’t save at the bank, the only mistake you made was the one highlighted below in the print

  • I recommend wrapping your code inside a Try catch block to know if the problem is at the time of inserting

Show 9 more comments

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;
}

?>
  • This trick would break a branch ? Because it is an internal system here in the company so it can be very simple. A question, where you put 'seu_input' would be the name of select ?

  • 1

    exactly, and as the value returned it enters the case responsible for its value, this is a simple way to do everything by php, and for beginners as well

  • No need to mount the query in each case, just take the name of the table and mount the query at the end.

  • Exactly that

0

if the name of your <select> for table do so:

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

Obs: this if you are using the 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 names of the fields in your table and the names as variable, and the method of your form has to be as post

  • $sql="INSERT into SISTEMA1". $_POST[table]." ` set version='". $_POST[???? ]." ???????? '"; mysql_query($sql); I’m kind of a beginner with PHP, would that be about right? and where I put the question marks what I would have to put there ? and in the option I would have to put some value to pass the parameter ?

Browser other questions tagged

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