Undefined array key, cant understand why the php file seems to not get the value in the html form

This is just for a school project and it feels like such a simple problem but every time i google what seems to be the problem i just cant understand most of the answers

<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>

this is the form in 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>";
                }
            }

and this is my incomplete php code, the goal is to let the user choose with a dropdown menu between 3 categories in my book table in my database. its either they search by Author, title or isbn. But i cant even get to that part without getting this “undefined array key” error in the first few lines

EDIT: The next project i was supposed to work on involved prepared statements, the school just wanted us to use manual insertions i guess

Answer

You don’t use an anchor to submit a form. You have to use a submit button. So change

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

to

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

When you use the anchor, none of the form fields are added to the URL.

You also have some problems in your PHP logic.

You can combine the tests for whether the parameters are set and properly filled in by using !empty(). You can test both parameters at once, rather than using nested if statements.

Your code is also wide open to SQL injection. You should use a prepared statement with parameters rather than substituting the variable into the 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';
}