Query with MySQL Options

0

I am working in a search engine where I can have two search options. The question is in how I do to make the query according to the values that the selection has.

<select>
    <option>Selecciona una Opcion</option>
    <option value="1">n id</option>
    <option value="2">Telefono</option>
</select>

The query should be:

  

SELECT * FROM people WHERE "here would go the option (id or phone)" LIKE $ variable.

Thanks for the help !!!

    
asked by angel ramirez 11.10.2018 в 21:32
source

2 answers

1

I'll leave you an example that I hope will guide you:

first you need to have a form where to show your options of query or filter in your case is a select:

<form action="consultar.php" method="POST">
<select name="filtro" id="filtro" required>
   <option selected disabled>Seleccione:</option>
    <option value="1">Id</option>
    <option value="2">Telefono</option>
</select>
<input type="text" name="variable" id="variable" required>
<input type="submit" value="Consultar">
</form>

File that receives your filter or search parameter ( query.php ):

<?php
$filtro = $_POST['filtro'];
$variable = $_POST['variable'];
if($filtro == 1){
$query =" SELECT * FROM gente WHERE id LIKE $variable ";
}
if($filtro == 2){
$query =" SELECT * FROM gente WHERE telefono LIKE $variable ";
}
...
...
..

?>

what is done in the file is to receive the parameter sent by your form, then using an if in this case valid filter or parameter and as armo or create the statement sql ($ query) ...

I hope I will guide you .. !!

    
answered by 11.10.2018 / 22:00
source
1

With PHP and jQuery:

<select>
<option id="opcion">Selecciona una Opcion</option>
<option value="1">n id</option>
<option value="2">Telefono</option>
</select>

<script>
opv = $("#opcion").val(); 
$.post( "accion.php", { op: opv}, function( data ) {
});
</script>

accion.php

<?php
$opv = $_POST["op"];
$campo="";

switch($opv)
{
    case 1:
    $campo=" campo_id ";
    break;  
    case 2:
    $campo=" campo_telefono ";
    break;
}

$str= "SELECT * FROM gente WHERE $campo LIKE ...."
?>
    
answered by 11.10.2018 в 21:59