Cómo evitar el primer resultado en blanco al buscar desde databse

2020-06-30 php html mysql

Tengo una página web de búsqueda, pero cuando se ejecuta la búsqueda, hay un primer resultado en blanco.

<?php include "headnav.php";

$count = 0;
$sql = "SELECT * FROM lost_property";
if (!empty($_POST)) {
    $name = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['name']));
    $item = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['item']));
    $area = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['area']));
    $sql = "
    SELECT * 
      FROM lost_property
      JOIN item 
        ON lost_property.itemID = item.itemID
      JOIN area 
        ON lost_property.areaID = area.areaID
     WHERE name LIKE '%$name%' 
       AND item LIKE '%$item%' 
       AND area LIKE '%$area%'
     ORDER 
        BY lost_property.name ASC
";
    $search_query = mysqli_query($dbconnect, $sql);
    $count = mysqli_num_rows($search_query);
}
$result = $dbconnect->query($sql);
?>

<body>
    <div class="form">
        <h1>Search for lost property here:</h1>
        <form action="" method="POST">
            Name:
            <input type="text" placeholder="Name" name="name">
            Item type:
            <select name="item" class="dropdown">
                <option value="" disabled selected>Item</option>
                <?php
                $item_sql = "SELECT DISTINCT item FROM `lost_property`
                JOIN item ON (lost_property.itemID = item.itemID)
                ORDER BY item ASC
                ";

                $item_query = mysqli_query($dbconnect, $item_sql);
                $item_rs = mysqli_fetch_assoc($item_query);
                do {
                ?>
                    <option value="<?php echo $item_rs['item']; ?>"><?php echo $item_rs['item']; ?></option>
                <?php
                } while ($item_rs = mysqli_fetch_assoc($item_query));
                ?>
            </select>
            Area (where it was found):
            <select name="area" class="dropdown">
                <option value="" disabled selected>Area</option>
                <?php
                $area_sql = "SELECT DISTINCT area FROM `lost_property`
                JOIN area ON (lost_property.areaID = area.areaID)
                ORDER BY area ASC
                ";

                $area_query = mysqli_query($dbconnect, $area_sql);
                $area_rs = mysqli_fetch_assoc($area_query);
                do {
                ?>
                    <option value="<?php echo $area_rs['area']; ?>"><?php echo $area_rs['area']; ?></option>
                <?php
                } while ($area_rs = mysqli_fetch_assoc($area_query));
                ?>
            </select>
            <input type="submit" value="Search" name="btn">
        </form>
    </div>
    <div class="gallery">
        <h2>Search results:</h2>
        <?php
        //check for results. If there are none display error
        if ($count < 1) {
        ?>
            <div class="error">
                <h1>No results were found.</h1>
            </div>
            <?php
        } //end if
        else {
            do {
            ?>
                <!-- display image and information from database and show in gallery  -->
                <div class="results">
                    <h3><?php echo $search_rs['name']; ?></h3>
                    <h3><?php echo $search_rs['item']; ?></h3>
                    <p><?php echo $search_rs['area']; ?></p>
                </div>
        <?php
            } // end of do
            while ($search_rs = mysqli_fetch_assoc($search_query));
        } //end else
        //if there are any display
        ?>
    </div>
    </table>
</body>

</html>

Es difícil de ver sin CSS, pero no importa lo que se busque, siempre hay un resultado, pero los campos h3 y p no tienen ningún contenido. Si no apareciera el mensaje de error sin resultados, también aparecería allí. ¿Qué está causando este primer resultado?

Answers

Use while (){} , si usa do en su lugar, se ejecutará una vez primero (crédito a Magnus Eriksson).

Termina como

else {
            while ($search_rs = mysqli_fetch_assoc($search_query)) {
            ?>
                <!-- display image and information from database and show in gallery  -->
                <div class="results">
                    <h3><?php echo $search_rs['name']; ?></h3>
                    <h3><?php echo $search_rs['item']; ?></h3>
                    <p><?php echo $search_rs['area']; ?></p>
                </div>
        <?php
            } // end of do
        } //end else
        //if there are any display
        ?>

en vez de

else {
            do {
            ?>
                <!-- display image and information from database and show in gallery  -->
                <div class="results">
                    <h3><?php echo $search_rs['name']; ?></h3>
                    <h3><?php echo $search_rs['item']; ?></h3>
                    <p><?php echo $search_rs['area']; ?></p>
                </div>
        <?php
            } // end of do
            while ($search_rs = mysqli_fetch_assoc($search_query));
        } //end else
        //if there are any display
        ?>

Related