查询并显示mysql搜索结果

时间:2015-11-06 22:26:30

标签: php mysql database mysqli

我正在尝试在我的网站上创建一个搜索栏,它将查询mysql数据库并以时尚的方式返回结果。搜索将用于查找名称。例如,用户可以键入" John Doe"关于John Doe的任何信息都将被退回。不幸的是,每当我点击提交按钮并且我的网站转换为' search.php'页面完全空白。我已经测试了我与数据库的连接,并知道它正在运行。我的代码有什么问题,绝对没有显示出来吗?它甚至不会将用户恢复为index.html。

我的index.html上有一个搜索栏,如下所示:

       <form id="navsearch" class="form-group" role="search" action="search.php" method="post">
            <div class="input-group" display="inline">
                <input name="search" type="text" value="Search" class="form-control" placeholder="Search">
                <span class="input-group-btn">
                    <button class="btn btn-default btn" name="submit" class="btn btn-lg" type="submit">
                        <span class="glyphicon glyphicon-search" name="submit"></span>
                    </button>    
            </div>  
        </form>

这是关于&#39; search.php&#39;:

的php
 <?php

 //make connection to database

  $connection = mysqli_connect('xxx.xxxx', 'root', 'hello', 'awstutorial', 3306);

 $search_word = '';
 if (isset($_POST['submit'])) {
   if (!empty($_POST['search'])) {
    $search_word = $_POST['search'];
    $search = mysqli_real_escape_string($connection,      $_POST['search']);

    $query = "SELECT * FROM data WHERE name LIKE '%$search%' ";
    $search_query = mysqli_query($connection, $query);

    // START RESULTS

    if ($result = mysqli_query($connection, $query)) {
        $to_display = "<div>";


        while ($row = mysqli_fetch_array($result)) {
            if ((substr($row['file'], -3) == 'jpg') || (substr($row['file'], -3) == 'JPG') || (substr($row['file'], -3) == 'PNG') || (substr($row['file'], -3) == 'png') || (substr($row['file'], -3) == 'GIF') || (substr($row['file'], -3) == 'gif'))
                $img_url = "uploads/" . $row['file'];
            else {
                if ((strtolower(substr($row['file'], -3) == 'png')) || (strtolower(substr($row['file'], -3) == 'doc')) || (strtolower(substr($row['file'], -3) == 'docx') ))
                    $img_url = "uploads/pdf_logo.png";
                else
                    $img_url = "uploads/no_doc.png";
            }
            $to_display .= "<a href='story.php?id=".$row['id']."'  class='col-md-3 asdivs portfolio-box fancybox fancybox.iframe'><img alt='' class='img-responsive' src='" . $img_url . "'><div class='portfolio-box-caption'>
                            <div class='portfolio-box-caption-content'>
                                <div class='project-category text-faded'>
                                    Read
                                </div>
                                <div class='project-name'>"
                        . $row['name'] . "'s Story
                                </div>
                            </div>
                        </div>" . $row['name'] . " - " . $row['email'] . "</a>";

        }
    }

    $to_display .= "</div><div style='clear:both;'></div>";
} else {
    header("Location: index.html");
   }
 }

?>

1 个答案:

答案 0 :(得分:0)

我很难确定提交表单时$search_query的含义是什么 - 我能想到的最好的是它指的是一个ajax请求,它提交搜索而不重新加载页面和结果然后以某种方式显示给同一页面上的用户?由于没有ajax的证据,很难说。

正如我在评论中提到的,您的查询引用$result但是记录集的迭代引用$to_display - 这些变量需要是相同的变量,这可能就是您面临空白的原因屏幕 - 以及变量error_reporting从未回显到浏览器的事实。

如有疑问,请使用<?php /* Sessions? */ session_start(); error_reporting( E_ALL ); /* Change this for live site! */ if( $_SERVER['REQUEST_METHOD']=='POST' ){ $conn = mysqli_connect('xxx.xxxx', 'root', 'hello', 'awstutorial', 3306); $exts_img=array('jpg','png','gif','jpeg'); $exts_doc=array('pdf','doc','docx'); $html=array(); /* use this to construct output. More efficient that string concatenation. */ if ( isset( $_POST['submit'] , $_POST['search'] ) && !empty( $_POST['submit'] ) && !empty( $_POST['search'] ) ) { /* It is better to use prepared statements or pdo */ $search = mysqli_real_escape_string( $conn,$_POST['search'] ); $sql = "SELECT * FROM `data` WHERE `name` LIKE '%".$search."%';"; $results = mysqli_query( $conn, $sql ); if( $results && mysqli_num_rows( $results ) > 0 ){ $html[]="<div>"; while( $row = mysqli_fetch_object( $results ) ) { $id = $row->id; $file = $row->file; $email = $row->email; $name = $row->name; $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ); if( in_array( $ext, $exts_img ) ) { $img_url = "uploads/" . $file; } else { if( in_array( $ext, $exts_doc ) ) $img_url='uploads/pdf_logo.png'; else $img_url='uploads/no_doc.png'; } $html[]=" <a href='story.php?id=".$id."' class='col-md-3 asdivs portfolio-box fancybox fancybox.iframe'> <img alt='' class='img-responsive' src='" . $img_url . "' /> <div class='portfolio-box-caption'> <div class='portfolio-box-caption-content'> <div class='project-category text-faded'>Read</div> <div class='project-name'>{$name}'s story</div> </div> </div> {$name} - {$email} </a>"; } $html[]="</div><div style='clear:both'></div>"; } /* No results */ $html[]='<h1>Sorry, no results...</h1>'; } else { header("location: index.html"); } /* free resources */ mysqli_free_result( $results ); mysqli_close( $conn ); /* output stuff to the browser */ echo implode( PHP_EOL, $html ); } ?> 突出显示可能出错的地方。

-
相关问题