gpt4 book ai didi

php - "Undefined Index"表单提交后出错

转载 作者:行者123 更新时间:2023-11-30 22:06:14 26 4
gpt4 key购买 nike

在这个页面中,我有一个表单,并且我有来自上一页的 $_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 变量都存在 undefined index 错误,因此我的查询无法发送。以下是 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);
}

}

?>

最佳答案

当您提交表单时,前一个表单中的 $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"]; }

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

关于php - "Undefined Index"表单提交后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41668995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com