Search Bar with Location dropdown sumit both results - PHP / mySQL - php

I have a basic search bar that searches my database using keywords (k). I am trying to submit both location and search result but the problem is when I submit the search results I get "search.php?k=builder&k=New+York" and not "search.php?k=builder+New+York" how do I correct this?
HTML
<form action="search.php" method="get" style="margin:0 auto; text-align:center;">
<input type="text" name="k" size="10" style="width:40%;"/>
<div id="the-basics">
<input class="typeahead" type="text" name="k" placeholder="States of USA">
</div>
<input type="submit" value="Search" style="width:100px;">
</form>
Search Bar PHP Code:
<?php
if(!empty($_GET['k'])){
//if search bar is empty
}else{
echo "'<meta http-equiv='refresh' content='0;url=index.php'>";
//redirect back to index.php
}
$k = $_GET['k']; //k stands for keyword
$i = 0;
$terms = explode(" ", $k);
$query ="SELECT * FROM record WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "company_name LIKE '%$each%' " . "OR website LIKE '%$each%' " . "OR email LIKE '%$each%' " . "OR tel LIKE '%$each%' " . "OR description LIKE '%$each%'" . "OR location LIKE '%$each%'" . "OR description LIKE '%$each%'";
}
$connection = mysql_connect('localhost', 'username', 'password');
mysql_select_db('DB_name');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) { { ?>
..........search result shows here........

Hi you need appaly some JS
<script>
function submitForm(obj){
var search = $('input[name="k"]).val();
var state= $('#state').val();
$('input[name="k"]).val(search+" "+state )
obj.submit();
}
</script>
<form action="search.php" method="get" style="margin:0 auto; text-align:center;" onsubmit=submitForm(this)>
<input type="text" name="k" size="10" style="width:40%;"/>
<div id="the-basics">
<input class="typeahead" type="text" id="state" placeholder="States of USA">
</div>
<input type="submit" value="Search" style="width:100px;">
</form>

Related

How to select certain term depending on search?

So I am wanting the user to be able to search by either keyword or ID number. If they search "test" right now for example it will pull all the entries with test which is what I want it to do for the keyword part of the search. However, I also want the user to be able to search my specific a specific ID# and just pulling that specific entry. I am unsure how I would go about doing this. I tried doing some sort of OR statement but it did not pull any entries.
Search box form
<div class ="search" id="browse">
<p> Find your appointment below or search by keyword</p>
<form id="" class="searchbar" action="searchAppt.php" method="get">
<input type="text" name="terms" size="40" class = "sbar" placeholder="Search by issue keyword or ID" oninput="validity.valid||(value='');"
onblur="if (this.value == '') {
this.value = 'Enter keyword or ID';
}"
onfocus="if (this.value == 'Enter keyword or ID') {
this.value = '';
}"/>
<button type="submit" class = "btn">Search</button>
</form>
</div>
searchAppt.php
if (filter_has_var(INPUT_GET, "terms")) {
$terms_str = filter_input(INPUT_GET, 'terms', FILTER_SANITIZE_STRING);
} else {
echo "There were no appointments found.";
include ('includes/footer.php');
exit;
}
//explode the search terms into an array
$terms = explode(" ", $terms_str);
$sql = "SELECT * FROM appointments WHERE 1";
foreach ($terms as $term) {
$sql .= " AND email = '". $_SESSION['email'] ."' AND issue LIKE '%$term%' OR id ='%term%'
";
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<br /><br /><center><h1>My Ticket(s)</h1><br />
<div class='table'>
<div class='tr'>
<div class='td'><b>Ticket #</b></div>
<div class='td'><b>Issue</b></div>
<div class='td'><b>Date</b></div>
<div class='td'><b>Ticket Details</b></div>
</div>";
// output data of each row
while($row = $result->fetch_assoc()) {
$starttimepast = strtotime($row["start_time"]); //converts date time received from MySQL into a string
$datepast = date("m/d/y", $starttimepast);
echo "<div class='tr'>
<div class='td'>".$row["id"]."</div>
<div class='td'>".$row["issue"]."</div>
<div class='td'>".$datepast."</div>
<div class='td'><form action='ticketdetails.php' method='post'>
<input type='hidden' name='id' value='".$row["id"]."'>
<input type='submit' value='Ticket Details'></form>
</div>
</div>";
}
echo "</div>";
echo "<br /><center><a href='myProfile.php'><h4>Go back to my profile</h4></a></center>";
include ('includes/footer.php');
} else {
echo "<br /> <br /><center><h3>Your search <i>'$terms_str'</i> did not match any appointments</h3></center>";
echo "<center><a href='myProfile.php'><h4>Go back to my profile</h4></a></center>";
echo "<br />";
exit;
}
?>
<?php
// clean up resultsets when we're done with them!
$query->close();
// close the connection.
$conn->close();
Perhaps it will help to explicitly group the terms:
$sql = "SELECT * FROM appointments WHERE email = '" . S_SESSION['email'] . "'";
$exprs = array();
foreach ($terms as $term) {
$exprs[] = "(issue LIKE '%$term%' OR id LIKE '%$term%')";
}
if (!empty($exprs)) {
$sql .= ' AND (' . join(' OR ', $exprs) . ')';
}
The result in this case will include records that matched any of the terms.
Note: It would be good to use a DB API like laravel/PDO/mysqli to simplify the query building and properly escape the values.

Search multiple Criteria - And/or search in PHP

Not sure what I am doing wrong here. I would like to create a search that allows the user to do an and or search.
However when I use the below code, if I type in Brown as Colour1 it will return all results, same as post code.
The goal is to allow the user to search multiple fields to return a match. So Colour1 and Postcode
<html>
<head>
<title> Logo Search</title>
<style type="text/css">
table {
background-color: #FCF;
}
th {
width: 250px;
text-align: left;
}
</style>
</head>
<body>
<h1> National Logo Search</h1>
<form method="post" action="singlesearch2.php">
<input type="hidden" name="submitted" value="true"/>
<label>Colour 1: <input type="text" name="criteria" /></label>
<label>Colour 2: <input type="text" name="criteria2" /></label>
<label>PostCode: <input type="text" name="criteria3" /></label>
<label>Suburb: <input type="text" name="criteria4" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%')
or
('Colour2' like '%$criteria2%')
or
('PostCode' = '%$criteria3%')
or
('Suburb' like '%$criteria4%')
LIMIT 0,5";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
echo "<table>";
echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";
}
echo "</table>";
}// end of main if statment
?>
</body>
</html>
I can get it to work correctly when I use a dropdown list to select the criteria however I would like them to have multiple options to filter results.
<form method="post" action="multisearch.php">
<input type="hidden" name="submitted" value="true"/>
<label>Search Category:
<select name="category">
<option value="Colour1">Main Colour</option>
<option value="Colour2">Secondary Colour</option>
<option value="PostCode">Post Code</option>
</select>
<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
echo "connected " ;
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE $category LIKE '%$criteria%'";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
In general you run your query with AND condition because it has criteria and you have it means that you have to show value by matching each criteria with receptive column. but it also depends on users or client how they want to show their field.
In short it doesn't have any rule how to show.
Played around with it for a bit and found the answer. I needed to define the criteria. see below code
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$criteria2 = $_POST['criteria2'];
$criteria3 = $_POST['criteria3'];
$criteria4 = $_POST['criteria4'];
$criteria5 = $_POST['criteria5'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%')
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')

Searching multiple terms with php

I'm trying to get my search form to search for multiple terms.For example "Black dog".In my $keywords they are separated with commas (Black, dog).I know that i can just enter (Black dog, ) in my $keywords but I want them to be separated with commas.If I put single term (dog) it returns result for only that term.If I put 2 (Black dog) it returns 0 results.
<form class="form-inline pull-xs-right" action="search.php" method="POST" >
<input class="form-control" type="text" name="search" placeholder="Search">
<button class="btn btn-success-outline" name="searchh" type="submit" value=">>" >Search</button>
</form>
<?php
$i=null;
$search=null;
$result_query=null;
if(isset($_POST['search'])){
$get_value = addslashes($_POST['search']);
$search = mysqli_real_escape_string($con,"$get_value");
?>
<h2>Showing results for
<?php echo $_POST['search'] ?></h2>
<?php
if($search==''){
echo "<center><b>Please write something in the search box!</b></center>";
exit();
}
$search = $_POST['search'];
$terms = explode(" ", $search);
foreach ($terms as $each) {
$i++;
if ($i == 1)
$result_query .= "keywords LIKE '%$each%' ";
else
$result_query .= "OR keywords LIKE '%$each%' ";
}
$result_query = "SELECT * FROM content WHERE keywords LIKE '%".$search."%'";
$run_result = mysqli_query($con,$result_query);
// echo mysqli_num_rows($run_result);
// exit;
if(mysqli_num_rows($run_result)<1){
echo "<center>Sorry no matches found</center>";
exit();
}
while($row_result=mysqli_fetch_array($run_result))
{
$name=$row_result['name'];
$keywords=$row_result['keywords'];
$image=$row_result['image'];
$link=$row_result['link'];
?>

looking for something like SQL LIKE Operator

i am looking for same code to search things in my table colmon.
i dont want its search something "like" the input search.
i want it to search the exactly Value in the table.
if user search "the walking dead season 3 episode 1"
its search exactly "the walking dead season 3 episode 1" in the "moviename" row..
and dont show me the all results look like thats..
my code now is :
<?php
function connect($host,$username,$database,$password){
$to_connect = mysql_connect($host,$username,$password) or die ("UNFinded ".$username. " DB !");
$db = mysql_select_db($database, $to_connect) or die (mysql_error());
return $db;
}
connect("localhost","idevice2_ariel","idevice2_ariel","ariel123456");
if (!isset($_POST['submit_form'])) {
echo '<form name="search_form1" method="POST" action="search.php">
<input type="text" name="search_name" value="search movie name" />
<input type="submit" value="Submit" name="submit_form" />
</form>';
} else {
$search_name = $_POST['search_name'];
$query = mysql_query ("SELECT * FROM members where `moviename` = $search_name");
$count = mysql_num_rows($query);
while($row = mysql_fetch_array($query)) {
$fname = $row['moviename'];
$lname = $row['links'];
print ''.$row['moviename'].'<br />';
}
}
?>
thanks you! ariel.
if (!isset($_POST['submit_form'])) {
echo '<form name="search_form1" method="POST" action="search.php">
<input type="text" name="search_name" value="search movie name" />
<input type="submit" value="Submit" name="submit_form" />
</form>';
} else {
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
$search_name = $_POST['search_name'];
$query = $mysqli->prepare("Select * from members where moviename = ? OR links = ?");
$query->bindParam('ss',$search_name,$search_name);
$query->execute();
$query->bind_result($link,$movieName);
while($query->fetch()) {
echo ''.$movieName.'<br />';
}
$query->close();
}
?>
Consider using prepared statements.

How to make search results appear on another page?

I have a big search on my home-page and when the user types in the text fields and clicks submit I want the results from my database to appear on another site in this case 'searchresults.php.' In this case 'really.html' is my homepage.
Here is my code:
What am I doing wrong?
Really.html
<center>
<form action="searchresults.php" method="post">
<input type="text" size="35" value="Job Title e.g. Assistant Manager"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb"
onfocus="if(this.value == 'Job Title e.g. Assistant Manager'){this.value =
'';this.style.color='#000'}" />
<input type="text" size="35" value="Location e.g. Manchester"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb"
onfocus="if(this.value == 'Location e.g. Manchester'){this.value =
'';this.style.color='#000'}" />
<input type="image" src="but.tiff" alt="Submit" width="60">
</form>
Searchresults.php
<html>
<body>
<?php
if(strlen(trim($_POST['search'])) > 0) {
//all of your php code for the search
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '$search' AND
location LIKE '$searchterm'";
$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) {
echo "<br>$row[0]<br>";
echo "$row[1]<br>";
echo "$row[2]<br>";
}
}
}
?>
</body>
</html>
Thanks!
You should add attr name to your input.
For example,
<input type="text" name="search" ... />
<input type="text" name="searchterm" ... />
Also don't forget about escaping input data using mysqL_escape_string function
The query is probably returning 0 results. Instead of
if ($result) {
Try
if (mysql_num_rows($result) >= 1) {
And in your query try...
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'";
This will be less strict and return a better result set.

Categories