-1

I am trying to create a search function in PHP. I am using following functions:

In show.php:

<form class="form-inline" action="?controller=address&action=searchAll" method="post">
    <div class="form-group row">
        <div class="col-xs-4">
            <input class="form-control" id="terms" name="terms" type="text" placeholder="Search terms">
        </div>
    </div>
    <div class="form-group row">
        <div class="col-xs-4">
            <button type="submit" class="btn btn-primary" ><span class="glyphicon glyphicon-search"> </span> Search</button>
        </div>
    </div>
</form>

This form sends a POST call to an controller: addressController.php

function searchAll(){
if (!empty($_POST['terms'])) {
        $terms=$_POST['terms'];
        $address=Address::searchByTerms($terms);
        $listaAddresses[]=$address;
        //var_dump($id);
        //die();
        require_once('Views/Address/show.php');
    } else {
        $listaAddresses=Address::all();

        require_once('Views/Address/show.php');
    }


}

And the controller should call the action searchByTerms to the model file Address.php and then open show.php again showing only the filtered items :

public static function searchByTerms($terms){
        $db=Db::getConnect();
        $select=$db->prepare('SELECT * FROM tb_direcciones   WHERE fn LIKE "%:fn%"');
        $select->bindValue('fn',$terms);
        $select->execute();

        $addressDb=$select->fetch();


        $address = new Address ($addressDb['id_address'],$addressDb['cia'], $addressDb['fn'], $addressDb['ln'], $addressDb['type'], $addressDb['phone'], $addressDb['fromto'], $addressDb['direccion'], $addressDb['latitud'], $addressDb['longitud']);
        //var_dump($address);
        //die();
        return $address;

    }

But something is wrong in my code then the error function is thrown.

mvasco
  • 4,965
  • 7
  • 59
  • 120

2 Answers2

0

$select->bindValue(1, "%$terms%", PDO::PARAM_STR);

is right but you must declare %$terms% out side bind Value

$terms= "%".$terms."%";
$select->bindValue(1, $terms, PDO::PARAM_STR);
Abdullah Ockba
  • 124
  • 1
  • 2
  • 8
0

I think your PDO::bindValue() function is wrong.

According th the PHP Documentation here: http://php.net/manual/es/pdostatement.bindvalue.php, the bindValue function should take as the first argument the parameter name in the form

:name

Example:

bindValue (":name", $value, $data_type);

So maybe the code should be like:

$select=$db->prepare('SELECT * FROM tb_direcciones   WHERE fn LIKE "%:fn%"');

// here the right form
$select->bindValue(':fn', $terms, PDO::PARAM_STR);

Or if you want to use the index form with the question mark of the bindValue function, the solution could be on the form:

$select=$db->prepare('SELECT * FROM tb_direcciones   WHERE fn LIKE "%?%"');

// here the right form
$select->bindValue(1, $terms, PDO::PARAM_STR);
lerichard_v
  • 401
  • 3
  • 8
  • Thank you for you time and effort, but I guess the problem is not on the query part of the code. It must be in some of the controller or model functions – mvasco Nov 22 '18 at 17:08
  • there is no php error message, it is calling the error function inside the controller, that means that something on any of both functions (controller and model) is wrong – mvasco Nov 22 '18 at 17:12