PHP multi option search query ....? - php

I'm trying with search box, I created a form as below:
<form method="post" action="search.php">
<select name="purpose">
<option value="" selected="selected">-any-</option>
<option value="For Sale">For Sale</option>
<option value="For Rent">For Rent</option>
</select>
<select name="type">
<option value="" selected="selected">-any-</option>
<option value="Bungalow">Bungalow</option>
<option value="Apartment">Apartment</option>
</select>
<select name="location">
<option value="" selected="selected">-any-</option>
<option value="Norway">Norway</option>
<option value="Itley">Itley</option>
</select>
<input type="submit" value="Search">
</form>
Search.php
I'm trying with these queries but getting problem:
$purpose=$_POST['purpose'];
$type=$_POST['type'];
$location=$_POST['location'];
If I put AND query like this:
SELECT * FROM test WHERE purpose='$purpose' AND location='$location' AND type='$type'
then it not filter one by one result its appear blank.
If I put OR query like this:
SELECT * FROM test WHERE purpose='$purpose' OR location='$location' OR type='$type'
Then it filter mix results.
I want if all selected it filter (purpose >> type >> location) AND if one selected then filter by this but exact result else show
Result not found!
EDIT Adding update made in comments:
I'm doing like this but it showing error:
$qry = "SELECT * FROM test WHERE 1=1";
if($purpose!="")
$qry .= " AND purpose='$purpose'";
if($location!="")
$qry .= " AND location='$location'";
if($type!="")
$qry .= " AND type='$type'";
while ($row = mysql_fetch_array($sql))
echo $row['purpose'];
echo $row['location'];
echo $row['type'];
I want if not match display result not found else it filter by all and one by one.

Put that after obtaining the variables:
$sql = array();
if (!empty($purpose)) {
$sql[] = "purpose='$purpose'";
}
if (!empty($type)) {
$sql[] = "type='$type'";
}
if (!empty($location)) {
$sql[] = "location='$location'";
}
$sql = implode(' AND ', $sql);
$sql = "SELECT * FROM test" . (!empty($sql)?: " WHERE " . $sql: '');
EDIT: After seeing your comments
First, you should make the connection to the database with the mysql_connect functions. Let's say, for example, that you have this piece of code:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
In the $link var now you have your mysql handle. This piece of code must go at the first of every page where you wanna use the mysql connection. Once you have the SQL sentence (in the code I first wrote, the $sql var), use this after:
$result = mysql_query($sql);
if (mysql_num_rows($result) === 0) {
echo 'Result not found';
}
while ($row = mysql_fetch_array($result)) {
echo $row['purpose'] . '<br/>';
echo $row['location'] . '<br/>';
echo $row['type'];
}

$qry = "SELECT * FROM test WHERE 1=1";
if($purpose!="")
$qry .= " AND purpose='$purpose'";
if($location!="")
$qry .= " AND location='$location'";
if($type!="")
$qry .= " AND type='$type'";

Related

Search bar with dropdown catagories. Search results not pulling from database and showing

So I have search bar that I'm hoping searches records in a mysql database and show them on a webpage. It should allow the user to choose the field they are searching under but it is is not showing the records the other end. Any ideas?
html:
<form action='recordresult.php' method='POST' name='form_filter' class="form-style-1" >
<b>Search</b><br>
<select name="selectVal">
<option value="category" >Select a category</option>
<option value="first_name">First Name</option>
<option value="surname">Surname</option>
<option value="address">Address</option>
<option value="phonenumber">Telephone</option>
</select>
<input type='text' name='search' placeholder='Enter text here...'><br>
<input type='submit' value='Send'>
</form>
PHP
<?php
include("config.php");
$link = mysqli_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysqli_error($link));
// select the database
mysqli_select_db($link, $database)
or die ("Could not select database because ".mysqli_error($link));
$search = isset($_POST['search']) ? htmlspecialchars(trim($_POST['search'])) : null;
$catLocation = isset($_POST['selectVal']) ? htmlspecialchars(trim($_POST['selectVal'])) : null;
$query = "SELECT * FROM $table WHERE ";
//YOU INDICATED YOU'D NEED TO RUN THE SEARCH-QUERY IF THE SEARCH-TERM AND SEARCH-SCOPE ARE DEFINED IE: NOT NULL; HOWEVER IF THE SEARCH TERM IS NOT GIVEN, YOU SELECT EVERYTHING IN THAT TABLE... (BAD PRACTICE, THOUGH)
if($catLocation){
if($search){
if($catLocation == "category"){
$query .= " category LIKE '%" . $search . "%'";
}
else if($catLocation == "first_name"){
$query .= "first_name LIKE '%" . $search . "%'";
}
else if($catLocation == "surname"){
$query .= "surname LIKE '%" . $search . "%'";
}
else if($catLocation == "address"){
$query .= "address LIKE '%" . $search . "%'";
}
else if($catLocation == "phonenumber"){
$query .= "phonenumber LIKE '%" . $search . "%'";
}
}
else{
$query .= "1";
}
$sql = mysqli_query($query);
//HERE AGAIN WAS AN ERROR... YOU PASSED mysql_fetch_array A STRING $query INSTEAD OF A RESOURCE: $sql
while ($row = mysqli_fetch_array($sql)){
$firstname = $row["first_name"];
$surname = $row["surname"];
$address = $row["address"];
$phonenumber = $row['phonenumber'];
echo "First Name : $firstname<br>";
echo "Surname : $surname<br>";
echo "Address : $address<br>";
echo "Phone Number: $phonenumber<br>";
}
}
?>
The code doesn't provide any errors just a blank area where it should be. Also wondering if anyone know if it's possible to have first_name and surname as fields and search say "Emma Watson" and to be able to return results from both fields if one of the words are in there?
Thanks for all your help!
Please check below updated code
include("config.php");
$link = mysqli_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysqli_error($link));
// select the database
mysqli_select_db($link, $database)
or die ("Could not select database because ".mysqli_error($link));
$search = isset($_POST['search']) ? htmlspecialchars(trim($_POST['search'])) : null;
$catLocation = isset($_POST['selectVal']) ? htmlspecialchars(trim($_POST['selectVal'])) : null;
$query = "SELECT * FROM $table WHERE ";
//**If you want to merge for first name and surname then you need to merge both query with OR condition as below**
if($catLocation){
if($search){
if($catLocation == "category"){
$query .= " category LIKE '%" . $search . "%'";
}
else if($catLocation == "name"){
$query .= " ( first_name LIKE '%" . $search . "%' OR surname LIKE '%" . $search . "%' ) ";
}
else if($catLocation == "address"){
$query .= "address LIKE '%" . $search . "%'";
}
else if($catLocation == "phonenumber"){
$query .= "phonenumber LIKE '%" . $search . "%'";
}
}
else{
$query .= "1";
}
$sql = mysqli_query($link, $query); // **Adding reference connection variable**
while ($row = mysqli_fetch_array($sql)){
$firstname = $row["first_name"];
$surname = $row["surname"];
$address = $row["address"];
$phonenumber = $row['phonenumber'];
echo "First Name : $firstname<br>";
echo "Surname : $surname<br>";
echo "Address : $address<br>";
echo "Phone Number: $phonenumber<br>";
}
}
Merge 2 fields (Firstname and surname) in single (name) for search in both fields
<form action='recordresult.php' method='POST' name='form_filter' class="form-style-1" >
<b>Search</b><br>
<select name="selectVal">
<option value="category" >Select a category</option>
<option value="name">name</option>
<option value="address">Address</option>
<option value="phonenumber">Telephone</option>
</select>
<input type='text' name='search' placeholder='Enter text here...'><br>
<input type='submit' value='Send'>
</form>

How to display certain rows from mysql with variable sent via HTML form

I'm trying to get certain data which meets the criteria from the database using AND condition with user searchable HTML form which sends the data to the search.
Code:
<?php
$conn = new mysqli('localhost', 'user', 'pass', 'db');
if ($conn->connect_error) die($conn->connect_error);
$conn->set_charset("utf8");
if (isset($_POST['Kohderyhmä']) &&
isset($_POST['Näytön aste']) &&
isset($_POST['Vaikutusten vahvuus']) &&
isset($_POST['Käyttökelpoisuus']) &&
isset($_POST['text']))
{
$Kohderyhmä = get_post($conn, 'Kohderyhmä');
$Näytön_aste = get_post($conn, 'Näytön aste');
$Vaikutusten_vahvuus = get_post($conn, 'Vaikutusten vahvuus');
$Käyttökelpoisuus = get_post($conn, 'Käyttökelpoisuus');
$text = get_post($conn, 'text');
$query = "SELECT * FROM `tietokanta`
WHERE Kohderyhmä='$Kohderyhmä' AND `Näytön aste`='$Näytön_aste' AND `Vaikutusten vahvuus`='$Vaikutusten_vahvuus' AND `Käyttökelpoisuus: luokka`='$Käyttökelpoisuus'";
}
$results = $conn->query($query);
if (!$results) die ("Database access failed: " . $conn->error);
$rows = $results->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$results->data_seek($j);
$row = $results->fetch_array(MYSQLI_ASSOC);
echo '<h3>' . $row['Nimi'] . '</h3><br />';
echo '' . $row['Kokonaisarvio'] . '<br />';
echo '' . $row['Kuvaus'] . '<br /><br />';
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<b>Kohderyhmä</b><br />
<select name="Kohderyhmä" style="width: 150px;">
<option value="Kaikki">Kaikki</option>
<option value="Pikkulapset">Pikkulapset</option>
<option value="Alle kouluikäiset">Alle kouluikäiset</option>
<option value="Alakouluikäiset">Alakouluikäiset</option>
<option value="Nuoret">Nuoret</option>
<option value="Perheet">Perheet</option>
<option value="Vanhemmat">Vanhemmat</option>
<option value="Työntekijät">Työntekijät</option>
</select>
<br />
<b>Näytön aste</b>
<select name="Näytön aste" style="width: 150px;">
<option value="Kaikki">Kaikki</option>
<option value="Vahva">Vahva</option>
<option value="Kohtalainen">Kohtalainen</option>
<option value="Heikko">Heikko</option>
<option value="Ei riittävää näyttöä">Ei riittävää näyttöä</option>
<option value="Ei arvioitu">Ei arvioitu</option>
</select>
<br />
<b>Vaikutusten vahvuus</b>
<select name="Vaikutusten vahvuus" style="width: 150px;">
<option value="Kaikki">Kaikki</option>
<option value="Vahva">Vahva</option>
<option value="Kohtalainen">Kohtalainen</option>
<option value="Heikko">Heikko</option>
<option value="Ei vaikutusta">Ei vaikutusta</option>
<option value="Ei arvioitu">Ei arvioitu</option>
</select>
<br />
<b>Käyttökelpoisuus</b>
<select name="Käyttökelpoisuus" style="width: 150px;">
<option value="Kaikki">Kaikki</option>
<option value="Vahva">Vahva</option>
<option value="Kohtalainen">Kohtalainen</option>
<option value="Heikko">Heikko</option>
<option value="Ei käyttökelpoinen">Ei käyttökelpoinen</option>
<option value="Ei arvioitu">Ei arvioitu</option>
</select>
<br />
<br />
Haku: <input type="text" name="text" />
<input type="submit" value="Hae" />
</form>
I haven't used PHP to contact database before so the PHP code is very messy.
I don't understand any more than the very basics from PHP, I haven't used variables or objects or anything complex before.
HTML form:
variable1
variable2
variable3
variable4
variable5
--->
PHP script:
select * from db
where variable1 and variable2 and variable3 and variable4
--->
display results matching the criteria
Current code causes this error message in error_log:
PHP Warning: mysqli::query(): Empty query in /home/user/public_html/folder/script.php on line 23
I have already tried over 15 different variations of variables and sql query in total and nothing has worked..
If we shorten your if (isset($_POST ... something you can clearly see. This instruction
$results = $conn->query($query);
is always executed, regardless of whether isset returns true or not.
if (isset($_POST['Kohderyhmä']) &&
...)
{
$Kohderyhmä = get_post($conn, 'Kohderyhmä');
...
$query = "SELECT * FROM `tietokanta`...."
}
$results = $conn->query($query);
So if only one field has not been filled out correctly, the error is always the same :
PHP Warning: mysqli::query(): Empty query in ....
This makes it difficult to determine where the fault really comes from.
Place the curly bracket } behind database logic.
if (isset($_POST['Kohderyhmä']) &&
...)
{
$Kohderyhmä = get_post($conn, 'Kohderyhmä');
...
$query = "SELECT * FROM `tietokanta`...."
$results = $conn->query($query);
if (!$results) die ("Database access failed: " . $conn->error);
$rows = $results->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$results->data_seek($j);
$row = $results->fetch_array(MYSQLI_ASSOC);
....
}
}
?>
create a short test program to test only the database. Set only really necessary data fields in the query
test.php
<?php
$conn = new mysqli('localhost', 'user', 'pass', 'db');
if ($conn->connect_error) die($conn->connect_error);
$conn->set_charset("utf8");
$Kohderyhmä = "KohderyTest"; // replace with really existing values
$query = "SELECT * FROM `tietokanta` WHERE Kohderyhmä='".$Kohderyhmä."' ";
$results = $conn->query($query);
if (!$results) die ("Database access failed: " . $conn->error);
while ($row = $results->fetch_assoc()) {
echo "<h3>" . $row['Nimi'] . "</h3><br />";
echo $row['Kohderyhmä'] ."<br /><br />";
}
$results->free();
?>
Add hardcoded variables $Näytön_aste = "reallyExistingValue"; , add query data field for data field and watch when it starts to stutter.
Also we can not see your function get_post()
If you mean the Wordpress function get_post(), your call to the function is wrong.
I can well imagine that the failure from the function get_post() comes.
And you always false or empty values assigns.
$Kohderyhmä = get_post($conn, 'Kohderyhmä');
assign it direct.
$post = $_POST;
if (isset($post['Kohderyhmä']) &&
...)
{
$Kohderyhmä = $post['Kohderyhmä'];
...
Also you are using all select fields from the <form>, in the query.
4 Select's with 8,6,6,6 options means
8x6x6x6 == 1728
1728 possibilities are you shure you have one datarecord where all values matches.
WHERE Ko...='$Ko...' AND `Näy...`='$Näy...' AND `Vai...`='$Vai...' AND `Käy...`='$Käy...'";
WHERE All four Datafields must match to get a result !!!!!!!!!!!!!
You have to find a combination where all four values simultaneously exist.
UPDATE
OP new question :
If you want empty or some named values stop searching for a value in
database.
required every single variable to be found in the database which it
didn't find because I couldn't set the variable and there is no value
for "Kaikki" in the database, the word "Kaikki" means all choices
below that choice in the HTML form and for that I need some PHP
Here comes the new test.php
1) don't do $post['Näytön aste']; In the form the name is
<select name="Näytön aste" style="...">.
This will translated by submit to
$post['Näytön_aste']; look at the underscore _
This must be done with all select name with spaces in the name !!
2) That was the reason why you get not all $_POST[....] values !
OK ?
3) replace in your form
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
with
<form action="testNew.php" method="POST">
testNew.php
<?php
$conn = new mysqli('localhost', 'user', 'pass', 'db');
if ($conn->connect_error) die($conn->connect_error);
$conn->set_charset("utf8");
$post = $_POST;
if (isset($post['Kohderyhmä']) &&
isset($post['Näytön_aste']) &&
isset($post['Vaikutusten_vahvuus']) &&
isset($post['Käyttökelpoisuus']))
{
$Kohderyhmä = $post['Kohderyhmä'];
$Näytön_aste = $post['Näytön_aste'];
$Vaikutusten_vahvuus = $post['Vaikutusten_vahvuus'];
$Käyttökelpoisuus = $post['Käyttökelpoisuus'];
} else { die ("No valid values"); }
$count = 0;
$and = "";
$query = "";
if (!empty($Kohderyhmä) && $Kohderyhmä !="Kaikki" ) {
if ($count > 0) { $and = " AND "; }
$count++;
$query = $query.$and."`Kohderyhmä`= '".$Kohderyhmä."'";
}
if (!empty($Näytön_aste) && $Näytön_aste !="Kaikki" ) {
if ($count > 0) { $and = " AND "; }
$count++;
$query = $query.$and."`Näytön aste`= '".$Näytön_aste."'";
}
if (!empty($Vaikutusten_vahvuus) && $Vaikutusten_vahvuus !="Kaikki" ) {
if ($count > 0) { $and = " AND "; }
$count++;
$query = $query.$and."`Vaikutusten vahvuus`= '".$Vaikutusten_vahvuus."'";
}
if (!empty($Käyttökelpoisuus) && $Käyttökelpoisuus !="Kaikki" ) {
if ($count > 0) { $and = " AND "; }
$count++;
$query = $query.$and."`Käyttökelpoisuus: luokka`= '".$Käyttökelpoisuus."'";
}
if ($count > 0) {
$query = "SELECT * FROM `tietokanta` WHERE ".$query;
} else {
$query = "SELECT * FROM `tietokanta`";
}
echo $query;
if ($results = $conn->query($query)) {
while ($row = $results->fetch_assoc()) {
echo "<h3>" . $row['Nimi'] . "</h3><br />";
echo $row['Kohderyhmä'] ."<br /><br />";
}
} else {
echo "with your choices no records were found";
}
$results->free();
?>
$query = "SELECT * FROM `tietokanta`
WHERE Kohderyhmä='{$Kohderyhmä}' AND `Näytön aste`='{$Näytön_aste}' AND `Vaikutusten vahvuus`='{$Vaikutusten_vahvuus}' AND `Käyttökelpoisuus: luokka`='{$Käyttökelpoisuus}'";
Replace your query with this, and try.

Dependent Drop down list Jquery, AJAX, PHP, MYSQL

I am trying to create a dependent drop down list but it doesnt seem to be populating after I make my first selection. Each selection will get data from a MySQL database. In order for the second drop down to have any options (other then a default "select option" value) the user would have to first make a selection on the first drop down After a lot of googling I am having a hard time finding a simple solution to this.
Here is what I have so far,
Drop down lists (I use PHP and MySQL here to generate and output the drop down lists in a getter.php and require_once into an index.php and echo out the drop down)
$accountOptions = "";
$facilityOptions = "";
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
die("Connection failed: " . mysqli_connect_error());
}
///ACCOUNTS/////
$accountQuery = "SELECT account_id, account_name FROM account";
$accountData = mysqli_query($dbc, $accountQuery);
//loop through data and display all accounts
while ($aRow = mysqli_fetch_array($accountData)) {
$accountOptions .="<option value=\"".$aRow['account_id']."\">" . $aRow['account_name'] . "</option>";
}
$accountDropDown=" <label>Accounts: </label><br>
<select name='account' id='account' onChange='getFacility(this.value)'>
<option selected='selected' disabled='disabled' value=''>Select account</option>
" . $accountOptions . "
</select>";
////FACILITIES/////
$facilityDropDown=" <label>Facility: </label><br>
<select name='facility' id='facility'>
<option selected='selected' disabled='disabled' value=''>Select facility</option>
</select>";
JQuery/AJAX
function getFacility(val) {
$.ajax({
type: "POST",
url: "getfacility.php",
data:'account_id='+val,
success: function(data){
$("#facility").html(data);
}
});
}
getfacility.php
//db connection..
if(!empty($_POST["account_id"])) {
$accountID = $_POST['account_id'];
$sql = "SELECT *, account.account_name FROM facility "
. "INNER JOIN account ON account.account_id = facility.account_id "
. "WHERE facility.account_id = '". $accountID ."'";
$data = mysqli_query($dbc, $sql);
echo "<option selected='selected' disabled='disabled' value=''>Select facility</option>";
while ($fRow = mysqli_fetch_array($data)) {
$facilityOptions .="<option value=\"".$fRow['facility_id']."\">" . $fRow['facility_name'] . "</option>";
}
$facilityDropDown=" <label>Facility: </label><br>
<select name='facility' id='facility'>
<option selected='selected' disabled='disabled' value=''>Select facility</option>
" . $facilityOptions . "
</select>";
}
Right now, when I make a selection on my first drop down, the second one does not populate with anything, where am I going wrong?
Make the changes in getfacility.php,
if your ajax showing 200 ok status and expected response in Network (Console)
//db connection..
if(!empty($_POST["account_id"])) {
$accountID = $_POST['account_id'];
$sql = "SELECT *, account.account_name FROM facility "
. "INNER JOIN account ON account.account_id = facility.account_id "
. "WHERE facility.account_id = '". $accountID ."'";
$data = mysqli_query($dbc, $sql);
echo "<option selected='selected' disabled='disabled' value=''>Select facility</option>";
while ($fRow = mysqli_fetch_array($data)) {
echo "<option value=\"".$fRow['account_id']."\">" . $fRow['account_name'] . "</option>";
}

Php/MySQL if statement that returns information based on value selected

[page1.php]
<form action="result.php" method="POST" style='color:white;font-family: __custom;'>
<input type="text" placeholder="Search.." name="search" /><br><br>
<center>
<select name="example">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<br><br>
</center>
<input type="submit" value="GO!" id="sub"/>
</form><br><br><br>
[result.php]
<?php
$searchq = $_POST['search'] // search textbox;
//Connect to mySQL and retrieve all contacts
$conErr = "Error connecting to server or database";
$con = mysqli_connect("host", "user", "password", "database");
// Check connection
if (mysqli_connect_errno()) {
echo $connErr;
}
if ($_POST['example'] == 'C') {
echo "You chose inCludes";
$result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '%$searchq%' ORDER BY name ASC");
while ($row = mysqli_fetch_array($result)) {
echo "<hr>";
echo "All information regarding Select Statement";
echo "<br>";
}
} else if ($_POST['example'] == 'A') {
echo "You chose EndsWith";
$result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '$searchq%' ORDER BY name ASC");
while ($row = mysqli_fetch_array($result)) {
echo "<hr>";
echo "All information based on Select Statement";
echo "<br>";
}
} else {
echo "Blah";
}
mysqli_close($con);
?>
</div>
What i'm trying to do is on page1.php the user searches for a keyword the chooses either A, B, or C from a dropdown. Based on the A, B, C the user goes to result.php and then the information is given based on the query.
The code above does not seem to be working [I do not get any results at all] [blank]. Please help.
If you want to search for something that ends with a string, you have to put the % wildcard at the beginning:
} else if ($_POST['example'] == 'A') {
echo "You chose EndsWith";
$result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '%$searchq' ORDER BY name ASC");
while ($row = mysqli_fetch_array($result)) {
echo "<hr>";
echo "All information based on Select Statement";
echo "<br>";
}
}
You missed the % wildcard after LIKE clause
else if ($_POST['example'] == 'A') {
echo "You chose EndsWith";
$result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '%$searchq%' ORDER BY name ASC");
while ($row = mysqli_fetch_array($result)) {
echo "<hr>";
echo "All information based on Select Statement";
echo "<br>";
}
}

Search results displaying all information in table

I am trying to search some data from a database and display it. However whenever I click on 'search' all the results in the table are displayed. Any way I can display only the information the user is searching?
This is the html code for the form.
<h2> Search </h2>
<form action = "search.php" method = "post" >
Search for: <input type = "text" name ="find" placeholder="Animal Type/Date"><span class="required"</span> in
<select NAME = "field">
<Option VALUE = "Animal Type"> Animal Type</option>
<Option VALUE = "dateseen"> Date Required</option>
</Select>
<input type= "hidden" name = "searching" value ="yes"/>
<input type= "submit" name = "search" value ="Search"/>
</form>
This is the PHP code I'm using.
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
//$find = strtoupper($find);
//$find = strip_tags($find);
//$find = trim($find);
$sql=mysqli_query($link, "Select * FROM Locations ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
//}
?>
For that you need to first grab the find field of your text box like this..
$searchKeyword = $_POST['find']; // Sanitize this value first !!
Next you need to pass it to your query... Change your yourcolumn to suit your column name.
$sql=mysqli_query($link, "Select * FROM Locations WHERE `yourcolumn` LIKE '%$searchKeyword%' ");
EDIT :
You could grab both fields and do a check..
if(!empty($_POST['find']) && !empty($_POST['field']))
{
//do your query like..
$searchKeyword = $_POST['find']; // Sanitize this value first !!
$searchKeyword2 = $_POST['field']; // Sanitize this value first !!
$sql=mysqli_query($link, "Select * FROM Locations WHERE `yourcolumn` LIKE '%$searchKeyword%' AND `yourcolumn2` LIKE '%$searchKeyword2%' ");
}
else
{
echo "The search criteria cannot be empty !";
}

Categories