I'm beginner in PHP MySQL. It's gonna be too long to explain, I having much not likely error
but something error in conditions
. I found that trigger the problem, I found that it's more likely in AND
, OR
, Input tag no value
.
Scenario
I have a search form with multiple inputs. Let's pretend this is the search form, I can't send image due to low reputation.
G-Number: [________] (This is Unique key in my database)
Name: [__________]
Virtual Number: [__________]
[Search button]
Using OR condition
Let's pretend that I input 123456
in G-Number will query like this:
________________________________________
| G-Number | Name | Virtual Number |
----------------------------------------------------------------
| 123456 | John | 001abc |
So if only input John
in Name, and 123456
in G-Number. The error here it shows all John
even I input 123456
in G number.
Sample Result like this
GNumber | Name | Virtual Number
123456 | John | 001abc
535243 | John | 002efg
965235 | John | 002efg
Using AND condition
Here I need to fill up all the search form so it can search it, If I leave a blank it will say base from my system that No result Found
Basically if I input only 123456
in G-Number and leave the input
blanks it can't search result so it's look like the code is this in SQL query
SELECT * FROM `subscribers` WHERE `fName` = '' AND `gNumber` = '123456' AND `virtualNumber` = '';
So basically the value of leave input form was space or like this ''
.
That's why I'm looking for help and tips what should I do here, I don't want to use **MySQLi**
and **PDO**
now. I'm still practicing **MySQL**
. All I want here function multiple keyword search.
Here is my codes:
**HTML FORM**
<form class="form-inline" role="form" method="GET" id="searchform">
<div class="form-group">
<input type="text" class="form-control" name="gnumber" placeholder="Number" style="margin-right: 2em;" >
<input type="text" class="form-control" name="name" placeholder="Input First or Last Name" style="width: 300px;margin-right: 2em;">
<input type="text" class="form-control" name="virtual" placeholder="Virtual Number" style="margin-right: 2em;">
<br/><h5 style="float:right;"><button type="submit" class="btn btn-theme02" name="search" ><i class="fa fa-check">Search</i></button></h5>
</div>
**THE PHP CODE**
if (isset($_GET['search'])) {
$gnum = htmlentities(mysql_real_escape_string($_GET['gnumber']));
$name = htmlentities(mysql_real_escape_string($_GET['name']));
$vr = htmlentities(mysql_real_escape_string($_GET['virtual']));
if (empty($globe) && empty($name) && empty($vr)) {
echo "<script> alert('Please select atleast 1 filter!')</script>";
exit;
} else {
$sql = "SELECT * FROM `subscribers` WHERE `fName` = '".$name."' OR `gNumber` = '".$gnum."' OR `virtualNumber` = '".$vr."' ";
}
if ($result=mysql_query($sql)) {
$query_num_rows = mysql_num_rows($result);
if($query_num_rows == 0){
echo "<script> alert('No Records Found!')</script>";
} else {
while($row=mysql_fetch_array($result)){
// echo of result table is here.
Thank you.
$sql = "SELECT * FROM subscribers
WHERE";
if (empty($globe) && empty($name) && empty($vr)) {
echo "<script> alert('Please select atleast 1 filter!')</script>";
exit;
}
if(!empty($name)){
$sql .append(`fName` = '".$name."');
}
if(!empty($gnum)){
$sql .append(`gNumber` = '".$gum."');
}
You have to follow such type of approach. I really do not know the Php syntax. But approach should be like this.