dongwopu8210 2017-06-26 21:04
浏览 22
已采纳

为什么我的搜索php代码无法正常工作?

I've encountered a problem where my php code doesn't seem to work, I have looked at it over and over again and I can't figure it out. I am working on a project which requires me to allow a potential user of my product (the website) to search for an event by type such as comedy or live band etc. This should allow a user to view all events of the type they type in to appear.

So far I have created this code:

<?php

//$output= NULL;

if(isset($_POST['submit'])){
    //connect db
    require_once('connect.php');
    $search = $_POST['search'];

    //query db
    $result = $mysqli->query("SELECT * FROM resit1617_events WHERE type = 
'$search'");

if($result->num_rows > 0){
    while($rows = $result->fetch_assoc())
    {
        $event = $rows['name'];
        $type = $rows['type'];
        $description = $rows['description'];
        $day = $rows['day'];
        $month = $rows['month'];
        $year = $rows['year'];
        $recommendations = $rows['recommendations'];
        $age = $rows['min_age'];

        $output ="Event: $event, Type: $type, Date: $day/$month/$year, Minimum Age: $age, Recommendations: $recommendations, Description: 
$description<br />";
        }
    }else{
        $output = "No Results";
    }
}
?>

Then I have my HTML code with php included to echo the output values but the code seems to not work

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>The Welders Arms</title>
    <link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
    <div id="topbar">
        <p><img src="img/logo.png" alt="logo"></p>
    </div>
    <div id="wrapper">
        <form method="POST" action="search.php">
            <label id="searchheading">Search Events in The Welders Arms</label>
            <br>
            <input type="text" placeholder="Search by type of event e.g. live band, comedy etc.." id="type" name="search">
            <input type="submit" id="searchbutton" value="Search">
            <br>
        </form>
<?php echo $output; ?>
    </div>
</body>

</html>

If anyone knows a solution to why it could not work please let me know as this is holding me back from continuing onward with my project.

Thank You!

PS. All of the code is in one file called search.php

  • 写回答

2条回答 默认 最新

  • duancuo1234 2017-06-26 21:21
    关注

    A simple solution would be to change your initial if:

    if(isset($_POST['submit'])){
    

    to:

    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['search'])){
    

    Which tests if your form was submitted with the right method and also tests if the search field exists.

    You should also consider protecting yourself against mysql injection attacks with prepared statements or at the very least escaping strings manually:

    $search = $mysqli->real_escape_string($_POST['search']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题