Search function using PHP and PDO not working











up vote
-1
down vote

favorite












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.










share|improve this question






















  • Can you give the error output?
    – Artem Ilchenko
    Nov 22 at 15:53










  • How do I get PHP errors to display?
    – GolezTrol
    Nov 22 at 15:55

















up vote
-1
down vote

favorite












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.










share|improve this question






















  • Can you give the error output?
    – Artem Ilchenko
    Nov 22 at 15:53










  • How do I get PHP errors to display?
    – GolezTrol
    Nov 22 at 15:55















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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.










share|improve this question













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.







php pdo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 15:51









mvasco

1,82132964




1,82132964












  • Can you give the error output?
    – Artem Ilchenko
    Nov 22 at 15:53










  • How do I get PHP errors to display?
    – GolezTrol
    Nov 22 at 15:55




















  • Can you give the error output?
    – Artem Ilchenko
    Nov 22 at 15:53










  • How do I get PHP errors to display?
    – GolezTrol
    Nov 22 at 15:55


















Can you give the error output?
– Artem Ilchenko
Nov 22 at 15:53




Can you give the error output?
– Artem Ilchenko
Nov 22 at 15:53












How do I get PHP errors to display?
– GolezTrol
Nov 22 at 15:55






How do I get PHP errors to display?
– GolezTrol
Nov 22 at 15:55














2 Answers
2






active

oldest

votes

















up vote
0
down vote














$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);





share|improve this answer





















  • 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 at 17:08










  • Sorry To Ask but action="?controller=address&action=searchAll" what suppose to do
    – Abdullah Ockba
    Nov 22 at 17:46










  • this param should call the function searchAll from the controller
    – mvasco
    Nov 22 at 17:51










  • pls. bear with me suppose to call Controller page in same page show.php or in different
    – Abdullah Ockba
    Nov 22 at 18:42












  • However if in same page or in different page USE $_GET to call controller=address&action=searchAll And then you can use $_POST
    – Abdullah Ockba
    Nov 22 at 19:35


















up vote
0
down vote













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);





share|improve this answer























  • 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 at 17:08










  • may you share the error message that is shown?
    – degreerichi
    Nov 22 at 17:09










  • 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 at 17:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434488%2fsearch-function-using-php-and-pdo-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote














$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);





share|improve this answer





















  • 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 at 17:08










  • Sorry To Ask but action="?controller=address&action=searchAll" what suppose to do
    – Abdullah Ockba
    Nov 22 at 17:46










  • this param should call the function searchAll from the controller
    – mvasco
    Nov 22 at 17:51










  • pls. bear with me suppose to call Controller page in same page show.php or in different
    – Abdullah Ockba
    Nov 22 at 18:42












  • However if in same page or in different page USE $_GET to call controller=address&action=searchAll And then you can use $_POST
    – Abdullah Ockba
    Nov 22 at 19:35















up vote
0
down vote














$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);





share|improve this answer





















  • 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 at 17:08










  • Sorry To Ask but action="?controller=address&action=searchAll" what suppose to do
    – Abdullah Ockba
    Nov 22 at 17:46










  • this param should call the function searchAll from the controller
    – mvasco
    Nov 22 at 17:51










  • pls. bear with me suppose to call Controller page in same page show.php or in different
    – Abdullah Ockba
    Nov 22 at 18:42












  • However if in same page or in different page USE $_GET to call controller=address&action=searchAll And then you can use $_POST
    – Abdullah Ockba
    Nov 22 at 19:35













up vote
0
down vote










up vote
0
down vote










$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);





share|improve this answer













$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);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 16:38









Abdullah Ockba

397




397












  • 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 at 17:08










  • Sorry To Ask but action="?controller=address&action=searchAll" what suppose to do
    – Abdullah Ockba
    Nov 22 at 17:46










  • this param should call the function searchAll from the controller
    – mvasco
    Nov 22 at 17:51










  • pls. bear with me suppose to call Controller page in same page show.php or in different
    – Abdullah Ockba
    Nov 22 at 18:42












  • However if in same page or in different page USE $_GET to call controller=address&action=searchAll And then you can use $_POST
    – Abdullah Ockba
    Nov 22 at 19:35


















  • 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 at 17:08










  • Sorry To Ask but action="?controller=address&action=searchAll" what suppose to do
    – Abdullah Ockba
    Nov 22 at 17:46










  • this param should call the function searchAll from the controller
    – mvasco
    Nov 22 at 17:51










  • pls. bear with me suppose to call Controller page in same page show.php or in different
    – Abdullah Ockba
    Nov 22 at 18:42












  • However if in same page or in different page USE $_GET to call controller=address&action=searchAll And then you can use $_POST
    – Abdullah Ockba
    Nov 22 at 19:35
















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 at 17:08




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 at 17:08












Sorry To Ask but action="?controller=address&action=searchAll" what suppose to do
– Abdullah Ockba
Nov 22 at 17:46




Sorry To Ask but action="?controller=address&action=searchAll" what suppose to do
– Abdullah Ockba
Nov 22 at 17:46












this param should call the function searchAll from the controller
– mvasco
Nov 22 at 17:51




this param should call the function searchAll from the controller
– mvasco
Nov 22 at 17:51












pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 at 18:42






pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 at 18:42














However if in same page or in different page USE $_GET to call controller=address&action=searchAll And then you can use $_POST
– Abdullah Ockba
Nov 22 at 19:35




However if in same page or in different page USE $_GET to call controller=address&action=searchAll And then you can use $_POST
– Abdullah Ockba
Nov 22 at 19:35












up vote
0
down vote













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);





share|improve this answer























  • 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 at 17:08










  • may you share the error message that is shown?
    – degreerichi
    Nov 22 at 17:09










  • 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 at 17:12















up vote
0
down vote













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);





share|improve this answer























  • 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 at 17:08










  • may you share the error message that is shown?
    – degreerichi
    Nov 22 at 17:09










  • 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 at 17:12













up vote
0
down vote










up vote
0
down vote









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);





share|improve this answer














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);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 17:08

























answered Nov 22 at 17:05









degreerichi

263




263












  • 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 at 17:08










  • may you share the error message that is shown?
    – degreerichi
    Nov 22 at 17:09










  • 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 at 17:12


















  • 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 at 17:08










  • may you share the error message that is shown?
    – degreerichi
    Nov 22 at 17:09










  • 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 at 17:12
















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 at 17:08




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 at 17:08












may you share the error message that is shown?
– degreerichi
Nov 22 at 17:09




may you share the error message that is shown?
– degreerichi
Nov 22 at 17:09












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 at 17:12




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 at 17:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434488%2fsearch-function-using-php-and-pdo-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu