I'm wondering if anyone has a spare set of eyes for a moment or two. The problem I see lies with my issets. It is ignoring the first initial isset($lastName) and seems to always choose isset($title). Also I should mention the values are being posted from a HTML form to ssearch against a surname or title in a DB.
My question is, can anyone help with the logic problem or is there another way of doing so ? I know its prone to SQL injection too, however it's on a local DB and I'll be looking into this closely quite soon. If anyone can make suggestions I am definitely interested to hear them.
As per suggestion the HTML form.
<form name="lookup" method="post" action="searchEmployeeList.php" autocomplete="off">
<fieldset>
<p>Conduct a search</p>
<table width="600">
<tr>
<td width="150">Surname:</td>
<td>
<input type="text" name="lastName" value="" maxlength="25" placeholder="Employees surname">
</td>
</tr>
<tr>
<td width="150">Title:</td>
<td>
<input type="text" name="title" value="" maxlength="25" placeholder="Job role">
</td>
</tr>
<tr>
<td></td>
<!--Blank row-->
<td>
<input type="submit" name="submit" value="Search now">
<input type="submit" name="show_all" value="Show all">
</td>
</tr>
</table>
</fieldset>
</form>
if (isset($_POST['lastName'])) {
include 'DBConDetails.php';
if (isset($lastName)) {
$sql = "SELECT * FROM employee_data Where last_name = '".$_POST['lastName'].
"'";
}
if (isset($_POST['title'])) {
$sql = "SELECT * FROM employee_data Where title = '".$_POST['title'].
"'";
}
$result = mysqli_query($con, $sql);
if ($result - > num_rows > 0) {
echo "<table id = 'searchResults'> < tr >
< td > ID < /td> < td > Name < /td> < td > Age < /td><td>Title</td > < td > Years of Service < /td> < td > Salary < /td> < /tr>";
//multiple echos plainly for readability
while ($row = $result - > fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row["employee_id"] . '</td>';
echo '<td>' . $row["first_name"] . ' ' . $row["last_name"] . '</td>';
echo '<td>' . $row["age"] . '</td>';
echo '<td>' . $row["title"] . '</td>';
echo '<td>' . $row["yos"] . '</td>';
echo '<td>' . $row["salary"] . '</td>';
echo '</tr>';
}
} else {
echo "I'm afraid we could not find any matches, try editing your criteria.";
}
echo "</table>";
}
If anyone should come across a similar problem in the future, the issue lay with both if statements being true, which lead to both executing and the latter overwriting the first if()
statement. Should have noticed that !