0

我在我正在工作的系统上创建了一个搜索工具栏,但它不执行我正在搜索的内容,即使在我的数据库表中找到我搜索的关键字,它也总是不返回任何结果。请帮我分析我的代码,我提前错过或错误的地方。这是我的代码。搜索.php

    <form method="post" action="search.php">
     <p><input type="text" name="keywords"><input type="submit" value="Search"></p>
       </form>
       <?php
       include 'connect/func.inc.php';
       if(isset($_POST['keywords'])){
       $suffix = '';
    //trim is for ignoring spaces on the input type text
    $keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));

    $errors = array();
    if(empty($keywords)){
        $errors[]='Please enter a search keyword';
        }
        else if (strlen($keywords)<0) { 
        //strlen is for the no. of char
        $errors[]='Please three or more characters';
        }else if (search_results($keywords) === false){
        $errors[]='Your search for '.$keywords.' returned no results';
        } 

        if (empty($errors)) {
        //search
        $results = search_results($keywords);
        $results_num = count($results);
        $suffix = ($results_num !=1) ? 's': '';
        echo '<p>Your search for<strong>'. $keywords.'</strong> returned <strong>'. $results_num .'</strong>result',$suffix, '</p>';
        foreach($results as $result) {
        echo '<p><strong>', $result['studId'], '</strong><br>', $result['fname'],  $result['mname'], $result['lname'],'</p>';

        }
            //print_r(search_results($keywords));
        } else {
            foreach($errors as $error) {
            echo $error, '</br>';
                                        }
        }
    }

   ?>

函数.inc.php

<?php
include 'db.inc.php';
function search_results($keywords) {
    $returned_results = array();
    $where = "";

    $keywords = preg_split('/[\s]+/', $keywords);
    //preg_split select evry word and ignore many spaces
    $total_keywords = count($keywords);
    foreach($keywords as $key=>$keyword){

        $where .= "`keywords` LIKE '%$keyword%'";   
            if($key != ($total_keywords -1)) {
                $where .= " AND ";
            }
    }
    //echo $where;
        $results = "SELECT `studId`, LEFT(`fname`, 20) as `fname`, LEFT(`lname`, 20) as `lname`, LEFT(`mname`, 20) as `mname` FROM tbl_student WHERE $where";
    //echo $results;

    $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;
    if($results_num === 0) {
        return false;
    } else {
    //get info into database
        while ($results_row = mysql_fetch_assoc($results)) { 
            $returned_results[] = array(
            'studId'=> $results_row['studId'], 
            'fname'=> $results_row['fname'], 
            'mname'=> $results_row['mname'], 
            'lname'=> $results_row['lname']);
            }
                return $returned_results;
        }
    }

?>

我的桌子是这样的。tbl_student

  studId    fname   mname   lname
  c-1111    peter   jan      yu
  c-1112    jane    trish    li
4

1 回答 1

0

从外观上看,您正在引用一个不存在的列。在您的数据库结构中,有一个名为“关键字”的列?我没看到。

从您在原始问题下的评论看来您应该改变

$where .= "`keywords` LIKE '%$keyword%'";

$where .= "`studId` LIKE '%$keyword%'";
于 2013-02-04T10:12:22.530 回答