Creating a Search Form in PHP

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.

  1. 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.

  2. You can use default option in your selects with empty value

    <option value="">Select none</option>

It will simplify your code in checks:

Instead of:

if($location!="location" && $category!="category" && $status!="status")

Can use:

 if($location && $category && $status)

  1. Read about escaping

  2. 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 need
  3. There 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



Leave a reply



Submit