I created a search bar in html that searches my mySQL database and displays the results for my search.
My problem is that if I haven't typed anything on the bar it will display the whole table when I do not want that.
My code is this
<form action="Artists.php" method="post">
<input type="text" name="searchdatabase"<br><br>
<input type="submit" name="searching" value="Search"><br><br>
and I am trying to do this
<?php
if( ! mysqli_num_rows($resultsearch) ) {
echo "Mysql error: " .mysqli_error($mysql); }
while($row = mysqli_fetch_array($resultsearch)):?>
<tr>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['lastname'];?></td>
</tr>
<?php endwhile;?>
But this is not working, I am trying to create an if statement on html (I cant use Javascript) so that my code will not display my entire table and will only display the search I wanted once I press the search button.
Why do you have endwhile? Just use {} for your loop, match your searchdatabase to one of your names in the mysql. If it doesn't appear in the database throw a error. Other wise have it display. Right now you are calling your whole table. $blah = $con, "Select * From somedata where searchdatabase = firstName"; if you need to order by a certain way order by asce or something like that hope that helps.
just make the input as 'required'
<input type="text" name="searchdatabase" required>
this way you cant leave the search bar unfilled.
</div>