I am currently writing a search form in PHP for an SQL query. The user can enter information in any field (aslong as one has information in). Once the form is submitted the mySQL table is searched based on the criteria that they submitted.
The problem I am having is that each IF statement is running not just the one that matches exactly, e.g. if I complete all 3 criteria I get a search for the first criteria, then the second and first and then again for the first second and third.
Any help is appreciated. (This is only for internal use so I know about the SQL security etc but it not important at this point).
<?php
require 'connectdb.php';
// if there is ONLY a value in the Floor Field
if (!empty($_POST['Floor']))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is ONLY a value in the Floor Field
if (!empty($_POST['Floor']))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is ONLY a value in the flat number Field
if (!empty($_POST['flatno']))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE flatnumber='$_POST[flatno]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is a value in the floor and flatno field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno'])))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// second if - if there is something in the floor, flatno and status field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno']) && (!empty($_POST['status']))))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]' AND status='$_POST[status]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is a value in the floor and status field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['status'])))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND status='$_POST[status]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// CLOSE CONNECTION TO DATABASE
mysqli_close($con);
?>