I'm finding it very hard to return database results with php pdo pagination in wordpress. I have a form on another page that sends the search data to search4.php where I want to display matching rows and have previous|next links. I get no results, and If I echo $search, it just says 'search'
Here is the relevant code so far:
//html form on another page
<form method="POST" action="<?www.example.com/search4 ?>">
Search:
<input type="text" name="search"
<input type="submit" name="search" value="search" /></form>
//search4.php relevant code
if(isset($_REQUEST["search"]) && $_REQUEST["search"] != "")
{
$search = htmlspecialchars($_REQUEST["search"]);
$pagination->param = "&search=$search";
echo $search;
$pagination->rowCount("SELECT * FROM stories WHERE stories.category LIKE
'%$search%' OR stories.genre = LIKE '%$search%'");
$pagination->config(3, 8);
$sql = "SELECT * FROM stories WHERE stories.category LIKE '%$search%' OR
stories.genre = LIKE '%$search%' ORDER BY SID ASC LIMIT $pagination-
>start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
...etc
You <input type=text> and your <input type=submit> has the same name... So button value is overriding your text value... that why is always "search".
change it to:
<input type="text" name="search_text"/>
<input type="submit" name="search_button" value="search" />
Now, on your search4.php you can access your search text using $_REQUEST["search_text"]
PD: You can remove the name attribute on the submit button too.
Related
I have a search bar on my page, however it is about half way down, and when you submit a search query, the page scrolls back to the top.
Is there a way to get the page to stay where the user was when they search? My search is on the same page, and all of the content also stays on the page.
This is my current search code:
$search=mysql_real_escape_string($_POST['search']);
//- Queries the table to get content from rows -
$QuerySelect = "SELECT thumbnail,image,title,category,description
FROM homepage
WHERE category LIKE '%$search%' ";
//----------------- QUERY the TABLE, store THE CONTENT IN A PHP VARIABLE -----------------
$result = mysql_query($QuerySelect);
HTML :
<div class="searchcontainer">
<form method="post" action="index.php">
<input type="text" class="input" name="search" size="40" placeholder="Search by Category...">
<input type="submit" class="button" name="Submit" value="Search" id="Submit" >
</form>
</div>
<?php
$numrows = mysql_num_rows ($result );
if($numrows == 0){
Javascript & JQuery code is fine.
If you are posting to the same page you are on, you can make your action be index.php#my-form and give your form an ID of my-form. Of course change my-form to whatever you want.
I have page which has a table and for each row a corresponding button. I am trying to select that specific row and place the data into the text fields of a form but, the query doesn't seem to be working. Any help is much appreciated. Thanks. Here's the code so far:
<?php
if (isset($_POST['add'])){
unset($_POST['add']);
$id =$_POST['bookID'];
$q = "SELECT * FROM book WHERE book.bookID = $id";
$query1 = $db->query($q);
$data = $query1->fetch(PDO::FETCH_BOTH);
}
?>
<form>
<form action = "basket.php" method="post">
<p><input type="text" name="title" value="<?php print($data['Title']); ? >"/></p>
</form>
<input type="submit" name="add" value="Add to Field" />
</form>
Change about i missed something.
$db->query($q);
$db->execute(); // this line is missing you have to execute
$data = $db->fetch(PDO::FETCH_ASSOC); // no point fetching both when you know the names.
My situation:
I have two different forms. One common search form where users can search for products by their name/description, and another form that lets users search for products by their location (postcode and city).
This is the html for my search form:
<form name="searchform" method="post" action="index.php?go" class="searchform">
<input type="text" name="search" value="" placeholder="Suchen..." class="field_search" id="tags">
</form>
and this is the html for my location-search form:
<form class="location" method="post" action="index.php?go_location">
<input type="image" src="img/location/location.png" width="30" height="30" id="location_image" title="Ortung aktivieren"/>
<input type="text" size="18" placeholder="PLZ, Ort" name="location" id="location" title="Standort angeben"/>
<input type="image" name="" value="" src="img/location/go.png" width="30" height="30" id="location_submit"/>
</form>
and the corresponding php:
if(isset($_POST['search'])){
if(isset($_GET['go'])){
if(preg_match("/[A-Z | a-z]+/", $_POST['search'])){
$input=$_POST['search'];
$currently_searching = true;
//connect to db
$sql="SELECT * FROM table WHERE Name LIKE '%".$input."%' OR Description LIKE '%".$input."%'";
//echo results
}}}}
elseif(isset($_POST['location'])){
if(isset($_GET['go_location'])){
$input_location=$_POST['location'];
$currently_locationing = true;
$sql="SELECT * FROM table WHERE Postcode LIKE '%".$input_location."%' OR City LIKE '%".$input_location."%' OR Combined LIKE '%".$input_location."%' OR Combined2 LIKE '%".$input_location."%'";
//echo results
}}}
Now, individually, they work fine.
What I would like to achieve is connecting these two forms in a way that lets users who are already searching for a certain string (via the common search form) use the location - search form to narrow the results down to those corresponding with the given postcode...
I hope this is clear. I thought something like: If a user uses the common search form, the
$currently_searching
variable becomes "true", so if this variable is true and the user is using the location - search form, then connect them... so I tried adding something like this to the php-statement:
elseif(isset($_POST['location']) && $currently_searching == true){
if(isset($_GET['go_location']) && $currently_searching == true){
if($currently_searching == true){
$input_location=$_POST['location'];
$currently_locationing = true;
//connect to db
$sql="SELECT * FROM table WHERE (Name LIKE '%".$input."%' OR Description LIKE '%".$input."%') AND (Postcode LIKE '%".$input_location."%' OR City LIKE '%".$input_location."%' OR Combined LIKE '%".$input_location."%' OR Combined2 LIKE '%".$input_location."%')";
//echo results
}}}}
It doesn't work though. I'd appreciate some help guys! Thanks in advance.
Here is a little trick. Add the id locationForm to your location form and searchForm to your search form, so it looks like this:
<form id="locationForm" class="location" method="post" action="index.php?go">
<input type="image" src="img/location/location.png" width="30" height="30" id="location_image" title="Ortung aktivieren"/>
<input type="text" size="18" placeholder="PLZ, Ort" name="location" id="location" title="Standort angeben"/>
<input type="image" name="" value="" src="img/location/go.png" width="30" height="30" id="location_submit"/>
</form>
<form id="searchForm" name="searchform" method="post" action="index.php?go" class="searchform">
<input type="text" name="search" value="" placeholder="Suchen..." class="field_search" id="tags">
</form>
Then add this javascript:
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(document).on('submit', '#locationForm, #searchForm',function(e){
var locationInput = $('#locationForm input[name="location"]').clone();
locationInput.attr('type','hidden');
var searchInput = $('#searchForm input[name="search"]').clone();
searchInput.attr('type','hidden');
$('#locationForm').prepend(searchInput);
$('#searchForm').prepend(locationInput);
});
</script>
The javascript will add the search field to the location form and visa versa before submitting. So whenever you submit one of the forms, you will always have both values.
EDIT
In your corresponding.php you could use something like this if there are two seperate queries needed.
if(isset($_POST['search']))
{
$sql="SELECT * FROM table WHERE Name LIKE '%".$input."%' OR Description LIKE '%".$input."%'";
//Execute query
//Fetch results
}
if(isset($_POST['location']))
{
$sql="SELECT * FROM table WHERE Postcode LIKE '%".$input_location."%' OR City LIKE '%".$input_location."%' OR Combined LIKE '%".$input_location."%' OR Combined2 LIKE '%".$input_location."%'";
//Execute query
//Fetch results
}
//Combine results of search and location query
//echo results
Or if it's possible to execute one query you can use this:
if(isset($_POST['search']) && isset($_POST['location']))
{
$sql="HERE YOUR QUERY WHERE YOU CAN USE $_POST['search'] AND $_POST['location']";
//Execute query
//Fetch results
}
//Combine results of search and location query
//echo results
I have a search form built in my website's header (I'm using Bootstrap as well). The purpose of the form is to query users, based on the input.
View
<form class="navbar-form pull-right" method="post" action="updates/search" name="search">
<input type="text" class="input-medium" size="50" placeholder="Search for users" style="padding-left:10px">
<button type="submit" class="btn btn-small" name="sub">Search</button>
</input>
</form>
Model
function get_search($input)
{
$this->db->like('username',$input);
$query = $this->db->get('users');
print_r($query->result()); // for now i am just printing the results, in the future i will simply return this
}
Controller
public function index($renderData="")
{
$this->load->model("user_model");
$this->load->model("updates_model");
$this->_render('pages/search');
}
When I run a search query, say "as;fa;sklfl;kasf", it still prints all of the usernames in the database (5), instead of a blank page. Is my get_search method not finding the $input variable for some reason?
Edit: I fixed it. I had the value in the form, not in the input
your text box not given any name
<input type="text" class="input-medium" size="50" placeholder="Search for users" style="padding-left:10px" />
to
<input type="text" name ="username" class="input-medium" size="50" placeholder="Search for users" style="padding-left:10px"/>
Then use your Query like-
$input = $this->input->post('username');
$this->db->like('username',$input);
$query = $this->db->get('users');
$this->db->like('username',$input);
$query = $this->db->get('users');
Where is the association of your like query with the variable $query?
You tell your framework to get all users and set it to $query.
$query = $this->db->get('users');
function get_search($input)
{
//you might want to add `%` on your like statement just add both/after/before the third paraeter
$this->db->like('username',$input,'BOTH');
$query = $this->db->get('users');
//check the query
print_r($this->db->last_query());
}
I am trying to make a code to search for data in more than one column , the code works only if I search in one column. when I tried to search in two columns it says that nothing is found in the database.
<form action="" method="post">
<input type="text" name="param" >
<input type="submit" name="submit" value="search">
</form>
if (isset($_POST['param'])) {
$param= trim ($_POST['param']);
$result = "SELECT * FROM table WHERE student_Name LIKE '%$param%' AND course_name LIKE '%$param%'";
I think you should use OR unless you want to check the 2 columns with the same parameter.
$result = "SELECT * FROM thecars WHERE student_Name LIKE '%$param%' OR course_name LIKE '%$param%'";
Also, this is very open to SQL injection. Sanitize your input before you pass it to the query.
You could do a concat and search via that.
$result = "SELECT * FROM thecars WHERE CONCAT(`student_Name`, `course_name`) AS `search`LIKE '%$param%'";
Your using the same $param variable to search on both columns. If your searching for 2 separate values provide a form that supports inputting 2 values. And then handle such input properly in your query formation.
<form action="" method="post">
<input type="text" name="param1" >
<input type="text" name="param2" >
<input type="submit" name="submit" value="search">
</form>
if (isset($_POST['param1']) || isset($_POST['param2'])) {
$param1 = trim ($_POST['param1']);
$param2 = trim ($_POST['param2']);
$result = "SELECT * FROM thecars WHERE student_Name LIKE '%$param1%' AND course_name LIKE '%$param2%'";
Also change your column aggregation in where clause to OR as suggested by others if your searching for similar outcome.