通过搜索栏php查询数据库

时间:2018-07-31 22:00:49

标签: php html css mysql

我想创建一个搜索栏,Final.php是用户输入数据库的每个事件的显示页面,但是我想添加一个搜索栏以查询列表以更快地找到他们想要的结果(该事件每个团队的名字和四个分数)。我不明白自己哪里出了问题,希望有人可以提供帮助。


<?php
$con = mysqli_connect("localhost", "id5052875_signuplogin", "Meganruby2") or die("cannot connect");
mysqli_select_db($con, "id5052875_signuplogin") or die ("couldnt connect");
$output = '';

    //collect
if (isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    print("$searchq");
    $query = mysqli_query($con, "SELECT * FROM event WHERE event name LIKE '%$searchq%'") or die("could not search");
    $count = mysqli_num_rows($query);
    echo($searchq);
    if($count == 0 ) {
        $output = 'There was no search';

    }else {
        while($row = mysqli_fetch_array($query)) {
            $event = $row['event name'];
            $num1 = $row['Score1'];
            $num2 = $row['Score2'];
            $num3 = $row['Score3'];
            $num4 = $row['Score4'];


            $output .= '<div> '.$event.' '.$num1.' '.$num2.' '.$num3.' '.$num4.'</div>';

            }

        }



    }
?>    
<form action="Final.php" method="post">
    <input type = "text" name = "search" placeholder = "search for event.."/>
    <input type = "submit" value = "search"/>
</form>

1 个答案:

答案 0 :(得分:0)

您的第一个条件,改变

if ( isset( $_POST['event'] ) ) {if ( isset( $_POST['search'] ) ) {

$searchq = $_POST['search'];$searchq = $_POST['search'];

搜索文本输入的名称为“搜索”,但由于某种原因,您正在寻找事件。

此外,请不要直接接受用户输入并将其插入数据库查询中。请注意SQL注入。这是常见的question,用于检查并了解有关防止它的更多信息。