Search through two inputs in the same form

1

I'm trying to do a multiple query, using two inputs in a single form but the moment I hit enter it brings me everything from the database regardless of the type of que buscas? or en donde? .

The code of my search is this:

<?php session_start();

require 'extras/config.php';
require 'functions.php';

comprobarSession();

$conexion = conexion($bd_config);
if (!$conexion) {
    header('Location: error.php');
}

if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['busqueda']) || !empty($_GET['busqueda2'])) {
    $busqueda = limpiarDatos($_GET['busqueda']);
    $busqueda2 = limpiarDatos($_GET['busqueda2']);

    $statement = $conexion->prepare('SELECT * FROM publications WHERE titulo LIKE :busqueda or locacion LIKE :busqueda2');

    $statement->execute(array(':busqueda' => "%$busqueda%", ':busqueda2' => "%$busqueda2%"));
    $resultados = $statement->fetchAll();

    if(empty($resultados)){
        $titulo = 'No se encontraron articulos con el resultado ';
    } else {
        $titulo = 'Resultados de la busqueda: ';
    }
}

require 'views/buscar.php';

?>

and this is my form:

<form class="form-search-home" name="busqueda" method="GET" action="<?php echo RUTA; ?>indexsearch.php">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>¿Que buscas?</label>
                                            <input type="text" class="form-control  input-lg" placeholder="Palabras clave" name="busqueda">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>¿En donde?</label>
                                            <input type="text" class="form-control input-lg" placeholder="Lugar" name="busqueda2">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <button type="submit" class="btn btn-t-primary btn-lg btn-theme btn-pill btn-block">Buscar</button>
                                </div>
                                <div class="text-center">
                                    <a href="#modal-advanced" data-toggle="modal">Encuentra el trabajo que quieres!</a>
                                </div>
                            </form>

and this is the result that comes with the URL

  

link

    
asked by Cesar Gutierrez Davalos 22.05.2017 в 03:12
source

2 answers

0

Try the bind_param

$statement = $conexion->prepare();

$statement= $db->prepare('SELECT * FROM publications WHERE (titulo LIKE ?) or (locacion LIKE ?)');
$statement->bind_param("ss", $busqueda, $busqueda2);
$statement->execute();
$resultados = $statement->fetchAll();
    
answered by 22.05.2017 в 10:08
0

What I recommend is that you capture with POST that way you ensure that the data is not empty, I leave you a fragment of the code that could serve you.

$buscas = $_POST[Busqueda];
$lugar =$_POST[Busqueda2];

    $statement = $conexion->prepare("SELECT * FROM publications WHERE titulo LIKE '%$buscas%' or locacion LIKE '%$lugar%'");
    $resultados = $statement->fetchAll();
    
answered by 22.05.2017 в 19:29