Creating a search form in PHP
try this out let me know what happens.
Form:
<form action="form.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
Form.php:$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
Edit: Cleaned it up a little more.Final Cut (my test file):
<?php
$db_hostname = 'localhost';
$db_username = 'demo';
$db_password = 'demo';
$db_database = 'demo';
// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
}
?>
</body>
</html>
PHP Simple Search Form
try this
FORM
<html>
<body>
<form action="result.php" method="get">
ID Number: <input type="text" name="idnumber"> <input type="submit" name="form_submit" value="Search"><br>
//PUT THE NAME FOR SUBMIT BUTTON
</form>
<p>Input ID number</p>
</body>
</html>
result.php //database connection
global $conn;
$servername = "localhost"; //host name
$username = "username"; //username
$password = "password"; //password
$mysql_database = "dbname"; //database name
//mysqli prepared statement
$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());
mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");
if(isset($_GET['form_submit']))
{
$IDNUMBER =$_GET['idnumber'];
$stmt = $conn->prepare("select * from your_table_name_here where identification_number=? ");
$stmt->bind_param('s',$IDNUMBER);
$stmt->execute();
$val = $stmt->get_result();
$row_count= $val->num_rows;
if($row_count>0)
{
$result =$val->fetch_assoc();
print_r($result);
}
else
{
echo "identification_number not Match";
}
$stmt->close();
$conn->close();
}
PHP: how to create a search form that will link to my search script
Code (a rough idea) :
<html>
<head><title>Search Form</title></head>
<body>
<form action="search.php" method="GET">
<input type="text" name="keyword" id="keyword width="50" value="" />
<input type="submit" value="Search"/>
</form>
</body>
</html>
And get the keyword from your PHP script like this :<?php
// script.php
$searchfor = $_GET['keyword'];
$file = 'users.txt';
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:<br />";
echo implode("<br />", $matches[0]);
}
else{
echo "No matches found";
fclose ($file);
}
?>
Creating a search function
Well, okay, I have several ideas about what you should change in your code.
I strongly recommend you to separate representative logic (html and echoing variables) from functionality like defining variables and handling database queries. It will help you a lot in future.
You can use default option in your selects with empty value
<option value="">Select none</option>
Instead of:
if($location!="location" && $category!="category" && $status!="status")
Can use: if($location && $category && $status)
Read about escaping
On your main question - you can create query by concatenation. I give you example and you can replace it with 'OR' or 'AND' for your needs:
$sql = 'SELECT * FROM properties WHERE ';
$scopes = [];
foreach([$location,$category,$status] as $column => $condition) {
if ($condition) {
$scopes[] = $column.' LIKE \'%.$condition.'%\'';
}
}
$scopes = implode(' AND ',$scopes);
$sql .= $scopes.';';
// ...do what you needThere is a lot more advices for coding but maybe you just present it like dead-simple example, so I skip it.
Creating a search filter with PHP
You need to get your $name from your form submission. Then generate a SELECT statement to grab all the records that match it.
$name = mysqli_real_escape_string($mysqli,$_POST['name']);
$stmt = $mysqli->prepare('SELECT * FROM `youTable` WHERE `name`=?');
$stmt->bind_param("s", $name);
$stmt->execute();
If you are choosing whether to search for a name, city, etc... You need to update your code accordingly.
Related Topics
Symfony 3.1.5 Warning: Sessionhandler::Read(): Session Data File Is Not Created by Your Uid
PHP Upload File Enhance Security
Mysqli_Store_Result() VS. MySQLi_Use_Result()
The Behaviour of the or Operator in PHP
What Is the Js Equivalent to the PHP Function Number_Format
Why Do PHP Array Examples Leave a Trailing Comma
Session_Start Seems to Be Very Slow (But Only Sometimes)
How to Call Curl_Setopt with Curlopt_Httpheader Multiple Times to Set Multiple Headers
PHP Class: Global Variable as Property in Class
PHP Using Regex to Get Substring of a String
A Better Way to Replace Emoticons in PHP
How to Increase by 1 All Keys in an Array
Random Float Number Between 0 and 1.0 PHP
How to Sort Null Values Last Using Eloquent in Laravel
Laravel: How to Use Multiple Pivot Table Relationships
PHP - Display a 404 Error Without Redirecting to Another Page