MySQL制作搜索功能不会起作用

时间:2017-04-03 11:13:52

标签: php mysql search

我正在尝试在我的网站中构建一个搜索功能,它允许您根据他们的姓名或姓氏查找用户,然后它将打印所有找到的名称。问题是,我根本不会工作。你能帮助我吗?

编辑:如果我尝试搜索某些内容,它总会给我一个没有找到任何内容的答案。

php代码:

<?php
    $search = $_POST["search"];
    if(isset($search)) {
        $findname = "SELECT `name` FROM Account WHERE $search = `name` 
                    OR SELECT `name` FROM `Account` WHERE $search = `surname` 
                    OR SELECT `name` FROM `Account` 
                    WHERE $search = `name` . `surname` 
                    OR SELECT `name` FROM `Account` WHERE $search = `name` . `surnameprefix` . `surname`";

        $findsurname = "SELECT `surname` from Account WHERE $search = `name` 
                        OR SELECT `surname` FROM `Account` WHERE $search = `surname` 
                        OR SELECT `surname` FROM `Account` WHERE $search = `name` . `surname` 
                        OR SELECT `surname` FROM `Account` WHERE $search = `name` . `surnameprefix` . `surname`";

        $result2 = mysql_query($findsurname);
        $result1 = mysql_query($findname);
            if($result1 == false && $result2 == false) {
                echo '<div id="searchresult"><h1>People found:</h1> <p>No one was found...</p>';
            } else {
                echo '<div id="searchresult">
                        <h1>People found:</h1>
                        <table id="searchtable">
                            while($result = mysql_fetch_array($result1)){
                                <tr id="searchtablerow">
                                    <td> $findname ' . '$findsurname</td>
                                    <td><img src="standard-profile-square.jpg" id="searchpicture"></td>
                                </tr>
                            }
                        </table>
                    </div>';
            }
        }
?>

html表单代码:

    <div id="searchform">
        <h1>Search friends:</h1>
        <form name="searchform" method="post" action ="searchlink.php">
            <input type="text" name="search" id="search" autofocus placeholder="e.g. John Smith..."></input> <br>
            <input type="submit" name="submitsearch" value="Search" id="searchbutton"></input>
        </form>
    </div>

先谢谢你们帮助我!

您诚挚的, UNHEIL

2 个答案:

答案 0 :(得分:0)

你最后的回音太长了。输出结果的php指令不会被解释,而是输出。

<?php
} else {
  echo '<div id="searchresult">
  <h1>People found:</h1>
  <table id="searchtable">';
  while($result = mysql_fetch_array($result1)){
    echo '<tr id="searchtablerow">
        <td> '.$findname.' '.$findsurname.'</td>
        <td><img src="standard-profile-square.jpg" id="searchpicture"></td>
      </tr>';
  }
  echo '</table></div>';
}
?>

如果您使用'for Strings,则必须将变量连接到其中。

答案 1 :(得分:0)

您的代码中存在很多错误。 SQL语句错误。这两个陈述基本上做同样的事情 除了他们返回不同的列。

where子句中的SELECT是无意义的。相反,在where子句中,您应该添加更多限定符,而不是select语句。

在SQL语句中,您使用PHP连接,这在SQL中不起作用。 PHP代码有许多语法和逻辑错误。您正在使用mysql函数而不是mysqli函数。 您永远不会打开与数据库的连接。

代码中的问题列表一直在继续。

这里有一些应该有效的代码,可以让你更接近你想要的东西。

<?php
    $host = 'your host name';
    $user = 'your db user name';
    $password = 'your db user name';
    $db_name = 'your db name';
    $search = $_POST["search"];
    if(isset($search)) {
        $db = mysqli_connect($host, $user, $password, $db_name);
        $wild_search = "%".$search."%";
        $findname = "SELECT `name`,`surname`
        FROM `Account` 
        WHERE `name` LIKE '".$wild_search."'
            OR `surname` LIKE '".$wild_search."'
            OR CONCAT(`name`, `surname`) LIKE '".$wild_search."'
            OR CONCAT(`name`, `surnameprefix`, `surname`) LIKE '".$wild_search."';";
        $query = mysqli_query($db,$findname);
        $results = mysqli_fetch_all($query);

        if($result) {
            echo "<div id='searchresult'>\n";
            echo "<h1>People Found</h1>\n";
            echo "<table id='searchresult'>\n";
            foreach($result as $rowno => $row) {
                echo "<tr class='searchtablerow'>\n";
                echo "<td>".$row['name'].", ".$row['surname']."</td>\n";
                echo "</tr>\n";
            }
            echo "</table>\n";
        } else {
            echo "<div id='searchresult'><h1>People Found:</h1>\n";
            echo "<p>No one was found.</p>\n";
            echo "</div>\n";
        }
    } else {
    }
?>

在phpAdmin中,尝试以下测试语句。在示例中用“smith”替换表中存在的名称:

SELECT `name`,`surname`
    FROM `Account` 
    WHERE `name` LIKE '%smith%'
        OR `surname` LIKE '%smith%'
        OR CONCAT(`name`, `surname`) LIKE '%smith%'
        OR CONCAT(`name`, `surnameprefix`, `surname`) LIKE '%smith%';