Search function using PHP and PDO not working
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
add a comment |
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
Can you give the error output?
– Artem Ilchenko
Nov 22 '18 at 15:53
How do I get PHP errors to display?
– GolezTrol
Nov 22 '18 at 15:55
add a comment |
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
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
php pdo
asked Nov 22 '18 at 15:51
mvascomvasco
1,92043166
1,92043166
Can you give the error output?
– Artem Ilchenko
Nov 22 '18 at 15:53
How do I get PHP errors to display?
– GolezTrol
Nov 22 '18 at 15:55
add a comment |
Can you give the error output?
– Artem Ilchenko
Nov 22 '18 at 15:53
How do I get PHP errors to display?
– GolezTrol
Nov 22 '18 at 15:55
Can you give the error output?
– Artem Ilchenko
Nov 22 '18 at 15:53
Can you give the error output?
– Artem Ilchenko
Nov 22 '18 at 15:53
How do I get PHP errors to display?
– GolezTrol
Nov 22 '18 at 15:55
How do I get PHP errors to display?
– GolezTrol
Nov 22 '18 at 15:55
add a comment |
2 Answers
2
active
oldest
votes
$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);
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
Sorry To Ask butaction="?controller=address&action=searchAll"
what suppose to do
– Abdullah Ockba
Nov 22 '18 at 17:46
this param should call the function searchAll from the controller
– mvasco
Nov 22 '18 at 17:51
pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 '18 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 '18 at 19:35
add a comment |
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);
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
may you share the error message that is shown?
– degreerichi
Nov 22 '18 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 '18 at 17:12
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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);
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
Sorry To Ask butaction="?controller=address&action=searchAll"
what suppose to do
– Abdullah Ockba
Nov 22 '18 at 17:46
this param should call the function searchAll from the controller
– mvasco
Nov 22 '18 at 17:51
pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 '18 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 '18 at 19:35
add a comment |
$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);
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
Sorry To Ask butaction="?controller=address&action=searchAll"
what suppose to do
– Abdullah Ockba
Nov 22 '18 at 17:46
this param should call the function searchAll from the controller
– mvasco
Nov 22 '18 at 17:51
pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 '18 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 '18 at 19:35
add a comment |
$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);
$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);
answered Nov 22 '18 at 16:38
Abdullah OckbaAbdullah Ockba
3927
3927
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
Sorry To Ask butaction="?controller=address&action=searchAll"
what suppose to do
– Abdullah Ockba
Nov 22 '18 at 17:46
this param should call the function searchAll from the controller
– mvasco
Nov 22 '18 at 17:51
pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 '18 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 '18 at 19:35
add a comment |
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
Sorry To Ask butaction="?controller=address&action=searchAll"
what suppose to do
– Abdullah Ockba
Nov 22 '18 at 17:46
this param should call the function searchAll from the controller
– mvasco
Nov 22 '18 at 17:51
pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 '18 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 '18 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 '18 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 '18 at 17:08
Sorry To Ask but
action="?controller=address&action=searchAll"
what suppose to do– Abdullah Ockba
Nov 22 '18 at 17:46
Sorry To Ask but
action="?controller=address&action=searchAll"
what suppose to do– Abdullah Ockba
Nov 22 '18 at 17:46
this param should call the function searchAll from the controller
– mvasco
Nov 22 '18 at 17:51
this param should call the function searchAll from the controller
– mvasco
Nov 22 '18 at 17:51
pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 '18 at 18:42
pls. bear with me suppose to call Controller page in same page show.php or in different
– Abdullah Ockba
Nov 22 '18 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 '18 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 '18 at 19:35
add a comment |
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);
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
may you share the error message that is shown?
– degreerichi
Nov 22 '18 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 '18 at 17:12
add a comment |
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);
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
may you share the error message that is shown?
– degreerichi
Nov 22 '18 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 '18 at 17:12
add a comment |
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);
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);
edited Nov 22 '18 at 17:08
answered Nov 22 '18 at 17:05
degreerichidegreerichi
1013
1013
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
may you share the error message that is shown?
– degreerichi
Nov 22 '18 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 '18 at 17:12
add a comment |
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
may you share the error message that is shown?
– degreerichi
Nov 22 '18 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 '18 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 '18 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 '18 at 17:08
may you share the error message that is shown?
– degreerichi
Nov 22 '18 at 17:09
may you share the error message that is shown?
– degreerichi
Nov 22 '18 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 '18 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 '18 at 17:12
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Can you give the error output?
– Artem Ilchenko
Nov 22 '18 at 15:53
How do I get PHP errors to display?
– GolezTrol
Nov 22 '18 at 15:55