提交表单后出现“未定义的索引”错误

时间:2017-01-16 03:22:44

标签: php mysql

在这个页面中我有一个表单,我从前一页获得了$ _GET值。

$event_id = $_GET["event_id"];
$event_host_id = $_GET["event_user_id"];
$host_username = $_GET["username"];
$event_name = $_GET["event_name"];
$sport = $_GET["sport"];
$description = $_GET["description"];
$location = $_GET["location"];
$event_date = $_GET["event_date"];
$formatted_event_date = date("D jS F Y g:i a", strtotime($event_date));
$number_of_tickets = $_GET["number_of_tickets"];
$tick_end_date = $_GET["tick_end_date"];
$formatted_t_e_d = date("D jS F Y g:i a", strtotime($tick_end_date));
$tickets_remaining = $_GET["tickets_remaining"];

页面加载正常,使用GET变量没有问题。 但是,当我提交表格时:

<form  action="give_feedback.php" method="POST">
    Rating<input name="rating" type="range" min="0" max="10" value="0" step="1" onchange="showValue(this.value)" />
    <span id="range">0 /10</span>
    <script type="text/javascript">
        function showValue(newValue) {
            document.getElementById("range").innerHTML=newValue + " /10";
        }
    </script><br><br>
    <textarea name="comment" rows="4" cols="30" selectionStart spellcheck="true" placeholder="Feedback comment"></textarea><br>
    <input type="submit" value="submit_feedback" name="submit_feedback" />
</form>

使用:

if(isset($_POST["submit_feedback"])) {
    if(true) {
        //DO FORM VERIFICATION HERE
        $rating = (int)($_POST["rating"]);
        $comment = mysqli_real_escape_string($connection, $_POST["comment"]);

        $query = "UPDATE `booking_dates` SET `rating` = '$rating', `comment` = '$comment' WHERE `user_id` = '$user_id' AND `event_id` = '$event_id'";
        $result = mysqli_query($connection, $query);
        check_query_result($result);
    }
}

以前完全正常的所有GET变量都有一个未定义的索引错误,因此我的查询无法发送。 以下是give_feedback.php文件的完整代码:

<?php 
require('includes/session.php');
require('includes/connect.php');
require('includes/functions.php');
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <link href="stylesheets/layout.css" media="all" rel="stylesheet" type="text/css" />

        <title>My account</title>
    </head>
    <body>
        <div id="Holder">
            <div>
                <img class="logo" src="images/sports%20world%20logo.png" width="400px" height="200px">
            </div>
            <div>
                <p class="logged_in">Logged in as <?php echo htmlentities($_SESSION["username"]); ?></p>
            </div>
            <div id="NavBar">
                <nav>
                    <ul>
                        <li><a href="LoggedInIndex.php">All Events</a></li>
                        <li><a href="create_event.php">Create Event</a></li>
                        <li class="selected"><a href="my_account.php">My account</a></li>
                        <li><a href="event_ratings.php">Event Ratings</a></li>
                        <li><a href="log_out.php">Log Out</a></li>
                    </ul>
                </nav>
            </div>
            <div id="Content">
                <div id="PageHeading">
                    <h1 id="Header">Give feedback on this event:</h1>
                </div>
                <?php
                $user_id = $_SESSION["user_id"];
                if(isset($_GET["event_id"])){
                    $event_id = $_GET["event_id"];
                }
                if(isset($_GET["event_user_id"])){
                    $event_host_id = $_GET["event_user_id"];
                }
                if(isset($_GET["username"])){
                    $host_username = $_GET["username"];
                }
                if(isset($_GET["event_name"])){
                    $event_name = $_GET["event_name"];
                }
                if(isset($_GET["sport"])){
                    $sport = $_GET["sport"];
                }
                if(isset($_GET["description"])){
                    $description = $_GET["description"];
                }
                if(isset($_GET["location"])){
                    $location = $_GET["location"];
                }
                if(isset($_GET["event_date"])){
                    $event_date = $_GET["event_date"];
                }
                $formatted_event_date = date("D jS F Y g:i a", strtotime($event_date));
                ?>

                <div class="boxed">
                    <div class="left">
                        <h3><u><?php echo $event_id; ?></u></h3>
                        <h3><u><?php echo $event_name; ?></u></h3>
                        <h4>Hosted By <?php echo $host_username; ?></h4>
                        <p><?php echo $description; ?></p>

                        <h5><a href="my_account.php">Back To My Events</a></h5>

                    </div>
                    <div class="right">
                        <table>
                            <tr>
                                <td class="table_left">Sport</td>
                                <td class="table_right"><?php echo $sport; ?></td>
                            </tr>
                            <tr>
                                <td class="table_left">Location</td>
                                <td class="table_right"><?php echo $location; ?></td>
                            </tr>
                            <tr>
                                <td class="table_left">Date</td>
                                <td class="table_right"><?php echo $formatted_event_date; ?></td>
                            </tr>

                        </table>
                    </div> 
                </div>
                <div class="bottom">
                    <h2>Your feedback:</h2>
                    <form  action="give_feedback.php" method="POST">
                        Rating<input name="rating" type="range" min="0" max="10" value="0" step="1" onchange="showValue(this.value)" />
                        <span id="range">0 /10</span>
                        <script type="text/javascript">
                            function showValue(newValue)
                            {
                                document.getElementById("range").innerHTML=newValue + " /10";
                            }
                        </script><br><br>
                        <textarea name="comment" rows="4" cols="30" selectionStart spellcheck="true" placeholder="Feedback comment"></textarea><br>
                        <input type="submit" value="submit_feedback" name="submit_feedback" />
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

<?php    
if(isset($_POST["submit_feedback"])){
    if(true){//DO FORM VERIFICATION HERE
        $rating = (int)($_POST["rating"]);
        $comment = mysqli_real_escape_string($connection, $_POST["comment"]);

        echo $rating;
        echo "<br>";
        echo $comment;

        $query = "UPDATE `booking_dates` SET `rating` = '$rating', `comment` = '$comment' WHERE `user_id` = '$user_id' AND `event_id` = '$event_id'";
        $result = mysqli_query($connection, $query);
        check_query_result($result);
    }

}

?>

2 个答案:

答案 0 :(得分:0)

当您提交表单时,前一个表单中的$ GET值将不会传递。您可以通过向每个$ GET变量添加if(isset())条件来避免错误。

      if(isset($_GET["event_id"])){ $event_id = $_GET["event_id"]; }
      if(isset($_GET["event_user_id"])){  $event_host_id = $_GET["event_user_id"]; }
        .
       ..


       ..

        .

        .

        .
        if(isset($_GET["tickets_remaining"])){ $tickets_remaining = $_GET["tickets_remaining"]; }

OR

将所有获取值传递给提交的表单。

答案 1 :(得分:0)

检查数据库表,包括每种特定数据类型的数据类型和长度。

相关问题