Kenny Baltscheit
Kenny Baltscheit

Reputation: 27

Creating a search filter with PHP

So I'm stuck on this,

    <input type="text" name="search">
    <select>
    <option value="Name">Name</option>
    <option value="Address">Address</option>
    <option value="company_address">company_address</option>
    <option value="contact_number">contact_number</option>
    </select>
<input type="submit">

<?php
include "connect.php";
$sql = "SELECT * FROM propertydetail";
x
?>

My question is, How can I use PHP to select a row from my database with filled information, by querying lets say option "name" out of one of my options. I need it to echo out all information from that name, even if it's given more than once in the table of my database.

$output = NULL;
$output .= '<div>'. $name . $address .'</div>';

echo $output;

I can format this better using a table within my php, So basically what I'm asking is, How would I select "Row from database"

by querying just a single name. Thanks in advance. I'm just a beginner, so don't judge me too hard.

Upvotes: 0

Views: 2612

Answers (2)

Andrew Coder
Andrew Coder

Reputation: 1058

This is all pretty basiic MySQL syntax. Check out codeacademy. Let's assume your database column names match the values within your select options. A very basic search would look like this:

<form METHOD="POST"><? // need to use POST and close form also at the end. ?>
    <input type="text" name="search">
    <select NAME="searchtype">...</select><? // Important you name the select! ?>
        <option value="Name">Name</option>
        <option value="Address">Address</option>
        <option value="company_address">company_address</option>
        <option value="contact_number">contact_number</option>
    </select>
    <input type="submit">
</form>

<?php
include "connect.php";
$type = mysqli_real_escape_string($_POST['searchtype']);
$search = mysqli_real_escape_string($_POST['search']);

$sql = "SELECT * FROM propertydetail WHERE ".$type." LIKE '%".$search."%'";

?>

Upvotes: 2

durbnpoisn
durbnpoisn

Reputation: 4669

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.

Upvotes: 0

Related Questions