douhe1864 2016-10-19 08:32
浏览 29
已采纳

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

  • 写回答

1条回答 默认 最新

  • douduoyan5943 2016-10-19 08:54
    关注

    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']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题