1

I have search box and button to search inside MySQL table. And it works well. But when I inset 0 in my search box it gaves me error message (that I set it): Enter a word to search . This is my HTML for search box and button :

<form method="post" class="form-inline my-2 my-lg-0 navbar navbar-expand-lg navbar-light bg-info">
            <input name="search" class="form-control mr-sm-2" placeholder="Search" aria-label="Search">
            <button name="submit-search" class="btn  my-2 my-sm-0 bg-white" type="submit">Search</button>
        </form>

And my function search():

function search()
{
    if (empty($_POST['search'])) {
        $_SESSION['message'] = "Enter a word to search";
        $_SESSION['msg_type'] = "warning";
        return;
    }
    $keyword = '%' . $_POST['search'] . '%';
    $database = new Database;
    $db = $database->dbConnection();
    $stmt = $db->query("SELECT * FROM posts WHERE id LIKE '$keyword' OR userId LIKE '$keyword' OR title LIKE '$keyword'  OR completed LIKE  '$keyword'");

    $count = $stmt->rowCount(); //num di record trovati 
    if ($count > 0) {
        $_SESSION['message'] = "Found items: " . $count;
        $_SESSION['msg_type'] = "success";
    } else {
        $_SESSION['message'] = "No article match with: " . $_POST['search'];//it results this message. I don't get it why don't recognize that character(0).
        $_SESSION['msg_type'] = "warning";
        //header("location: allart.php");
    } ?>

    <div class="container" style=" margin-top:5%;">
        <table class=" table table-hover" id="search-table" style="font-size: 18px;">
            <hr>
            <div style="text-align:center;"> <label style="text-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1), 10px 10px 10px rgba(0, 0, 0, 0.2); font-size:30px;border-radius: 45px; "> Results from search : </label></div> <br>
            <tr>
                <th>User ID:</th>
                <th> ID: </th>
                <th> Title: </th>
                <th> Compiled: </th>
            </tr>

            <?php while ($row = $stmt->fetch()) : ?>
                <tr>
                    <td> <?php echo $row['userId'] ?> </td>
                    <td> <?php echo $row['id'] ?> </td>
                    <td> <?php echo $row['title'] ?> </td>
                    <td> <?php echo $row['completed'] ? 'Compiled' : 'Not Compiled' ?> </td>
                </tr>
            <?php endwhile ?>
        </table>
    </div>
<?php }

It works well. But when I insert 0 in my search box it seems like he does not recognize the character shows the error message that I set it . I check my MySQL table and I found rows where id =10. So when I insert 0 in my search bar it should result that row with id = 10. I don't get it why it can't find any word that contains 0.

Any suggestion how to fix this? Thanks!

1 Answers1

0

Your problem is that from PHP's standpoint, 0 is basically 'empty'. Instead of checking for this...

if (empty($_POST['search'])) {

.. try checking for whether it's set or not:

if (!isset($_POST['search'])) {

See a demo of this approach here: https://3v4l.org/osfXH

Edit: I missed the exclamation mark, thanks! :)

Andrew
  • 827
  • 2
  • 6
  • 14