PHP数据库搜索显示

To give you an understanding of what I'm working on. I'm working on a project where on my school campus you can navigate around. For example a room can be called J2626 or J2617 etc... The thing is that when a user Is using my website I only want it to type in the exact name of the room to be able to display anything. Right now it doesn't work like I want it to. For example: if you only type in J in the search field, it will display all rooms that start with J.

Here is my code right now:

<?php

$fileName = dirname(dirname(__FILE__)) . "../../db/bth_nav.sqlite";

if (isset($_GET['page'])) {
    $argument = $_GET['page'];
    $argument = "%" . $argument . "%";

    $db = new PDO("sqlite:$fileName");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

    $stmt = $db->prepare('SELECT * FROM bth_nav WHERE name LIKE (:nameOwner) OR owner LIKE (:nameOwner);');
    $stmt->execute(array($argument));
    $res = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if (empty($res)) {
        header("Location: " . "http://" . $_SERVER['index.php']);
    }
?>

<div class = "formObjectSearch">

    <?php foreach ($res as $val) { ?>
            <div class = "infoTab">
                <h2><?= $val['name']; ?></h2>
                <p>Guidance: <?= $val['info']; ?></p>
                <p>Whos sitting here?: <?= $val['owner']; ?>
            </div>
            <img class = "buildingImage" src = "img/<?= $val['image']; ?>"
                <?php
      }
}
                ?>
    </div>

I've tried making diffrent if statements but with no success. So what I need help with is that I only want the user to be able to search for a correct room name and not be able to only search for J and display everything.

Update

Here is my index.php:

I used this link, because Code sample didn't want to work: index.php code

Here is my building.php:

Samer here: building.php code

And the the new search.php with help:

Same here: search.php code

I get these errors right now:

Notice: Undefined index: index.php in /path/incl/files/search.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /path/incl/header.php:7) in /path/incl/files/search.php on line 19

My header.php code:

Same here: header.php code

My config.php code:

Same here: config.php code

My footer.php code:

Same here: footer.php code

If i understanded you correctly and if you want to search for all rooms with starting letter J, you need to change your arguments.

Explanation:

'%J%' - searches for selected field, where is J letter.

'%J' - searches for selected field, where J is the last letter.

'J%' - searches for selected field, where J is the first letter.

Try to change your argument like that:

$argument = $argument . "%";

UPDATE

Fixing your if statement:

if (empty($res) OR $res == null) {
   header('Location: http://'.$_SERVER['index.php']);
}
else {
   echo '<div class = "formObjectSearch">';
   foreach ($res as $val) {
        echo '<div class = "infoTab">';
        echo '<h2>'.$val["name"].'</h2>';
        echo '<p>Guidance:'.$val["info"].'</p>';
        echo '<p>Whos sitting here?:'.$val["owner"].'</p>';
        echo '</div>';
        echo '<img class="buildingImage" src="img/'.$val['image'].'" />';
   }
   echo '</div>';
}

OR

You can try to replace empty, with:

if ($res->rowCount() > 0) {
  // if founded
} else {
  // header('Location: http://'.$_SERVER['index.php']);
}