首页 > 解决方案 > 未定义的数组键,无法理解为什么php文件似乎无法获取html表单中的值

问题描述

这只是一个学校项目,感觉就像一个简单的问题,但每次我用谷歌搜索似乎是什么问题,我就是无法理解大部分答案

<form action="bookResults.php" method="get">
    <h4>Book Search</h4>
    <label for="searchType">Search Type:</label>
    <select name="searchType" id="searchType">
        <option value="title">Title</option>
        <option value="author">Author</option>
        <option value="isbn">ISBN</option>
    </select><br>
    <label for="searchTerm">Search Term:</label>
    <input type="text" name="searchTerm"><br>
    <a class="btn btn-primary" href="bookResults.php" role="button">Submit</a>
</form>

这是html中的表格

<?php
if (!isset($_GET['searchType'])) {
    $searchType = $_GET['searchType'];
    if (!isset($_GET['searchTerm'])) {
        $searchTerm = $_GET['searchTerm'];
    
        echo $searchType;
        echo $searchTerm;
        if(!$searchType || $searchTerm){
            echo 'You have not entered search details. Please go back and try again';
        }else{
            $mysqli = new mysqli('127.0.0.1:3306','zero','1234','mp7');
            if ($searchType == 'title') {
                $query = "select * from book where title like '%".$searchTerm."%'";
                $result = $mysqli->query($query);
                $resultCount = $result->num_rows;
    
                echo "<p>Result for ".$searchType." : ".$searchTerm."   </p>";
                echo "<p>Number of books found: ".$resultCount."</p>";
                for($ctr = 0;$ctr<$resultCount;$ctr++){
                    $row = $result -> fetch_assoc();
                    echo "<div class='card col-4'>";
                    echo "  <div class='card-body'>";
                    echo "      <h6>".$row['title']."</h6>";
                    echo "      <p>By ".$row['author_name']."<br/>";
                    echo "      ".$row['isbn']."</p>";
                    echo "  </div>";
                    echo "</div>";
                }
            }

这是我不完整的 php 代码,目标是让用户在我的数据库中我的书表中的 3 个类别之间使用下拉菜单进行选择。他们要么按作者、标题或 isbn 搜索。但是我什至无法在前几行中得到这个“未定义的数组键”错误

编辑:我应该从事的下一个项目涉及准备好的陈述,我猜学校只是希望我们使用手动插入

标签: phphtmlmysql

解决方案


您不使用锚来提交表单。您必须使用提交按钮。所以改变

<a class="btn btn-primary" href="bookResults.php" role="button">Submit</a>

<button class="btn btn-primary" type="submit" role="button">Submit</button>

使用锚点时,不会将任何表单字段添加到 URL。

您的 PHP 逻辑也存在一些问题。

您可以使用 结合测试参数是否设置和正确填写!empty()。您可以同时测试两个参数,而不是使用嵌套if语句。

您的代码也对 SQL 注入开放。您应该使用带参数的准备好的语句,而不是将变量替换到 SQL 中。

<?php
if (!empty($_GET['searchType']) && !empty($_GET['searchTerm'])) {
    $searchType = $_GET['searchType'];
    $searchTerm = $_GET['searchTerm'];

    echo $searchType;
    echo $searchTerm;

    $mysqli = new mysqli('127.0.0.1:3306','zero','1234','mp7');
    if ($searchType == 'title') {
        $query = "select * from book where title like CONCAT('%', ?, '%')";
        $statement = $mysqli->prepare($query);
        $statement->bind_param("s", $searchTerm);
        $statement->execute();
        $result = $statement->get_result();
        $resultCount = $result->num_rows;
    
        echo "<p>Result for ".$searchType." : ".$searchTerm."   </p>";
        echo "<p>Number of books found: ".$resultCount."</p>";
        for($ctr = 0;$ctr<$resultCount;$ctr++){
            $row = $result -> fetch_assoc();
            echo "<div class='card col-4'>";
            echo "  <div class='card-body'>";
            echo "      <h6>".$row['title']."</h6>";
            echo "      <p>By ".$row['author_name']."<br/>";
            echo "      ".$row['isbn']."</p>";
            echo "  </div>";
            echo "</div>";
        }
    }
} else {
    echo 'You have not entered search details. Please go back and try again';
}

推荐阅读