Store multiple select in a single column with PHP/javascript + Mysql

Asked

Viewed 1,241 times

4

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I’m having trouble storing multiple select in the same table field. In this case, when filling the form and marking more than one select, it stores only one and the input quantity field that is in javascript does not store. How could I store everything

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $numero_mesa = $_POST['numero_mesa'];
    $pedido_refeicao = $_POST['pedido_refeicao'];
    $num_refeicao = $_POST['num_refeicao'];
    $pedido_bebida = $_POST['pedido_bebida'];
    $num_bebida = $_POST['num_bebida'];
        $sql="INSERT INTO mpedido(numero_mesa,pedido_refeicao,num_refeicao,pedido_bebida,num_bebida) VALUES('$numero_mesa','$pedido_refeicao','$num_refeicao','$pedido_bebida','$num_bebida')";
        $resultado_pedido = mysqli_query($link,$sql);

}
?>



<form method="post" action="pedidos.php">
                <header>
                    <h2>Fazer Pedido</h2>
                </header>
                <fieldset>
                    <label>
                        <span>Mesa</span>
                        <input type="text"id="numero_mesa" name="numero_mesa">
                    </label>
                    <label>
                        <span>Comanda:</span>

                    </label>
                    <span>Refeições/Bebidas/Sobremesas:</span>
                    <div class="pedidos">

                        <select name="pedido_refeicao" id="pedido_refeicao"class="selecionar">
                            <option selected disabled>Selecione</option>
                            <option >Costela de Tambaqui sem Espinha</option> 
                            <option  >Lombo de Tambaqui Frito sem Espinha</option>
                            <option >Caldeirada de Tambaqui sem Espinha</option>
                            <option >Caldeirada de Tucunaré</option> 
                            <option >Peixe no Tucupi com Camarão</option>
                            <option >Escabeche de Pirarucu</option>
                            <option >Escabeche de Tambaqui</option>
                            <option >Escabeche de Tucunaré</option>
                            <option >Tucunaré Frito</option> 
                            <option >Sardinha Frita</option>
                            <option >Jaraqui Frito</option>
                            <option >Pacu Frito</option> 
                            <option >Filé de Pirarucu Frito</option>
                            <option >Filé de Pirarucu a Milanesa</option>
                            <option >Guisado de Pirarucu</option>
                        </select>
                        <a class="add" href="#">+</a>
                        <hr>
                        Selecionados
                        <hr>
                        <div class="selecionados">

                        </div>
                    </div>
                   <br>
                    <div  class="pedidos">

                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                        <select name="pedido_bebida" id="pedido_bebida"class="selecionar">
                            <option selected disabled>Selecione</option> 
                            <option >Fanta Laranja 1l</option> 
                            <option >Fanta Laranja 2l</option> 
                            <option >Cola Cola 1l</option>
                            <option >Cola Cola 2l</option>
                            <option >Bare 2l</option> 
                            <option >Fanta Uva</option>
                            <option >Fanta Laranja</option>
                            <option >Sprit</option> 
                            <option >Cola Cola </option>
                            <option >Cola Cola zero </option>
                            <option >Guaraná Antarctica</option> 
                            <option >Guaraná Baré</option>
                            <option >Suco Goiaba</option> 
                            <option >Suco Manga</option>
                            <option >Suco Pessego</option>
                            <option >Suco Uva</option> 
                            <option >Suco Maracujá</option>
                            <option >Suco Laranja</option>
                            <option >Suco Caju</option> 
                            <option >Agua Mineral </option>
                            <option >Agua com Gas </option>
                            <option >Cerveja em Lata</option> 
                            <option >Limonada Natural</option>
                        </select>
                        <a class="add" href="#">+</a>
                        <hr>
                        Selecionados
                        <hr>
                        <div class="selecionados">

                        </div>

                    </div>
    <br>
    <button class="btn" type="submit">Fazer Pedido</button>   
                </fieldset>

            </form>
        </main>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script>

            $(".add").on('click',function(){ 
                var cont=0;
                var holder = $(this).closest('.pedidos');
                holder.find(".selecionados input").each(function(){
                    if($(this).val()==holder.find(".selecionar option:selected").html()){
                        cont++;
                    }
                });
                if(cont>0) { 
                    alert("Este item ja esta adicionado, altere a quantidade se deseja mais..");
                } else{
                    holder.find(".selecionados").append(
                        "<input disabled type='text' name='pedidos[]' value='" + 
                        holder.find(".selecionar option:selected").html() + 
                        "' ><input type='text' name='quantidade[]' placeholder='quantidade'><br>"
                    );
                }
            });


        </script>

    </body>
  • Friend to me your doubt is not clear.

  • well my system is of request . i will order several things , type can selects more than 5 things and wanted to put in my table all select

  • All options selected in the correct select?

2 answers

2


Good for storing multiple options of one select it must have the following structure:

<select name="pedido_bebida[]" id="pedido_bebida"class="selecionar" multiple>

That is to add the attribute multiple and inform that the name is an array.

Already to insert in the bank would be something like :

<?php
if($_POST)
{
    $pedido_bebida = implode(', ', $_POST['pedido_bebida']);
    $sql = "INSERT INTO sua_tabela(sua_coluna) VALUES ('$pedido_bebida')";
    mysqli_query($con, $sql) OR die(mysqli_error($con));
}
?>

The part of javascript if you’re gonna do the same thing...

<?php
if($_POST)
{
    $pedido_bebida = implode(', ', $_POST['pedido_bebida']);
    $quantidade = implode(', ', $_POST['quantidade']);
    $sql = "INSERT INTO sua_tabela(sua_coluna_bebida, sua_coluna_quantidade) VALUES ('$pedido_bebida', '$quantidade')";
    mysqli_query($con, $sql) OR die(mysqli_error($con));
}
?>
  • The javascript part is that the client doesn’t always want just one thing there when clicking on + it opens another select below and a text input next to select to place the item quantity , how can I make this javascript input go to bd ?

  • ma I have to enter in the database numero_mesa and select requedo_refeicao, num_refeicao,pedido_bebida,num_bebida como faço ?

  • So think every field of html has a name, the value of that input you will receive by php through the variable $_POST['name_input'] and vc can put in a variable), ai with this value you make a query sql or a select or an Insert.....

  • because that’s how it is , I updated the question of how he is at this time , I was doing the way you answered , he only input the drink and only one quantity he entered ,

  • this way he is only sending a name . wish he could send them all with implode

0

The same thing @Magichat said, using name of inputs in this way name="name-input[]" so you can add as many inputs as you want in the form and PHP will receive as if it were an array, but as was exemplified previously will be open a vulnerability very exploited in the web applications that is Sqlinjection so treat any variable that comes information from users before doing anything else with it.

  • The problem of SQLinjection in my view is not in the way the html receives the data and yes how you treat them in querie...

  • Yes exactly why when receiving the data in the backend they should be processed, for when they arrive in querys arrive in a "clean way".

Browser other questions tagged

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