how to use the case with a select coming from html

0

Good morning,

I need to pull data from the database, but wanted the user to choose from which data what value to search.

My code html :

<head>
    <title></title>
</head>
<body>
<form method="POST" name="frmBusca" action="Exibindo.php">

Pesquisar pedido:<input type="text" name="busca" id="busca">
<input type="submit" value="Buscar" name="buscar">
 <select name="opcao">
        <option>Numero bl</option>
        <option>Conteiner</option>
    </select>
</form>
</body>

Now there in php :

<?php 
$conexao = mysql_connect("localhost", "root", "") or die(mysql_error());
$banco = mysql_select_db("cliente_svn") or die(mysql_error());
$busca = $_POST['busca'];
$opcao = $_POST['opcao'];

case ($opcao = "Numero bl"):

I wanted to know the correct formatting to call case .

Thank you!

    
asked by anonymous 24.02.2017 / 14:16

2 answers

2

If I understand the question, you can do this:

HTML

<form method="POST" name="frmBusca" action="Exibindo.php">
    <label for="busca">Pesquisar pedido:</label>
    <input type="text" name="busca" id="busca">
    <select name="opcao">
        <option value="1">Numero bl</option>
        <option value="2">Conteiner</option>
    </select>
    <input type="submit" value="Buscar" name="buscar">
</form>

PHP

$conexao = mysql_connect("localhost", "root", "") or die(mysql_error());
$banco = mysql_select_db("cliente_svn") or die(mysql_error());
$busca = isset($_POST['busca']) ? $_POST['busca'] : '';
$opcao = isset($_POST['opcao']) ? $_POST['opcao'] : '';

switch($opcao){
    case '2':
        $column = "conteiner";
        break;
    default: // se for qualquer outro valor imprevisto, inclusive o 1
        $column = "num_bl";
}   

$query_select = "SELECT * FROM novo_pedido WHERE $column = '$busca'"; 

It would also be good to use mysqli .

Why should not we use 'mysql_ *' functions?

    
24.02.2017 / 16:31
0

I think I understand what you want, to be more dynamic would use jQuery, it's quite simple.

HTML

<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script></head><body><label>Pesquisarpedido:<inputtype="text" name="busca" id="busca"></label>
<input type="button" value="Buscar" id="buscar">
<select name="opcao">
   <option value="Numero_bl">Numero bl</option>
   <option value="Conteiner">Conteiner</option>
</select>
<div id="resultado"></div>
<script>
$(document).ready(function(){
   $("#buscar").click(function(){
     $("#resultado").load("arquivo.php");
   });
});
</script>
</body>

Put in the value of <options> the name of the columns exactly that you want to search for by selection.

file.php

 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "cliente_svn";
 $conn = new mysqli($servername, $username, $password, $dbname);
 if ($conn->connect_error) {
    die("A Conexão Falhou: " . $conn->connect_error);
 }


$busca = $_POST['busca']; //blablabla
$opcao = $_POST['opcao']; //Numero_bl
//digamos que o numero bl seja a coluna;

$select = "SELECT * from tabela where $opcao = '$busca'";
$result = $conn->query($select);
while($row = $result->fetch_assoc()){
   echo "<p>".$row['nome_da_coluna']."</p>";
}
    
24.02.2017 / 16:28