1

I have a database with only one table which has around 2 Million rows and 60 columns. I created a php application with multiple search filters like name, country, state, surname, domain etc which will filter and display the result in HTML table.

But the problem is, every text field is accepting only one input. I want to give multiple inputs in every text field separated by commas.

Eg: If I search for name: Harry Job: marketing I get the results . But i want a solution for City: Mumbai Job: marketing, Webdesigners, QA. (Multiple inputs)

Screenshot

Code:

Php/ Mysql Query Code

<?php
 $res = false;

 if(isset($_REQUEST['submit'])){
    $FirstName=$_POST['FirstName'];
    $LastName=$_POST['LastName'];
    $Function=$_POST['Function'];
    $State=$_POST['State'];
    $Country =$_POST['Country'];
    $Website =$_POST['Website'];
    $sql=" SELECT * FROM hz WHERE FirstName like '%".$FirstName."%' AND LastName LIKE '%".$LastName."%' AND Function LIKE '%".$Function."%' AND State LIKE '%".$State."%' AND Country LIKE '%".$Country."%' AND Website LIKE '%".$Website."%'";
    $q=mysqli_query($con, $sql);
}
else{
  $res = true;
}
?>

PHP Search Form:

<form method="post" class="search">
<table width="200">
<td>
   <tr><input class="form__input" type="search" name="FirstName" placeholder="First Name" value="<?php if(isset($FirstName)) echo $FirstName;?>" /></tr>
   <tr><input class="form__input" type="search" name="LastName" placeholder="Last name" value="<?php if(isset($LastName)) echo $LastName;?>" /></tr><BR>
   <tr><input class="form__input" type="search" name="Function" placeholder="Function" value="<?php if(isset($Function)) echo $Function;?>" /></tr><BR>
   <tr><input class="form__input" type="search" name="State" placeholder="State" value="<?php if(isset($State)) echo $State;?>" /></tr><BR>
   <tr><input class="form__input" type="search" name="Country" placeholder="Country" value="<?php if(isset($Country)) echo $Country;?>" /></tr><BR>
   <tr><input class="form__input" type="search" name="Website" placeholder="Website" value="<?php if(isset($Website)) echo $Website;?>" /></tr><BR>    
   <tr><input type="submit" name="submit" value=" Search " class="button"/></tr>
</td>
</table>

PS:

$con = mysqli_connect("host","user","password", "databasename");

I got suggestions like use explode functions, foreach etc, but no idea how to implement it in this code. What should I do?

  • Have you looked at the manuals for [`explode()`](http://php.net/manual/en/function.explode.php) and [`foreach()`](http://php.net/manual/en/control-structures.foreach.php)? They are quite straight forward. Read them and give it a try. – M. Eriksson Jan 27 '17 at 11:26
  • While you're at it, you should also read about [`Prepared Statements`](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) since your current code is wide open for [`SQL Injection attacks`](https://en.wikipedia.org/wiki/SQL_injection), which is bad, very very bad. – M. Eriksson Jan 27 '17 at 11:29
  • Is it? Damn this is my first PHP project and I suck! – user3382310 Jan 27 '17 at 11:31
  • 1
    It's OK to suck in your first project. It's how you develop skills over time that's important. – M. Eriksson Jan 27 '17 at 11:35

1 Answers1

0

I think this guy have the same problem as you do, and someone gave him a good answer: Creating a search form in PHP to search a database?

Community
  • 1
  • 1
Fahad
  • 113
  • 2
  • 15