您好,我是 php 的初学者,并坚持这一刻。我找不到问题。
Notice: Undefined index: post_id in C:\XA\htdocs\Passie Blog\post.php on line 22
这是问题所在的第 22 行:
$id = $_POST['post_id'];
这是我的 php 代码
<?php
if(!isset($_GET['id'])){
header('Location: index.php');
exit();
}else{
$id = $_GET['id'];
}
include('includes/db_connect.php');
if(!is_numeric($id)){
header('Location: index.php');
}
$sql = "SELECT title, body FROM posts WHERE post_id='$id'";
$query = $db->query($sql);
if($query->num_rows !=1){
header('Location: index.php');
exit();
}
if(isset($_POST['submit'])){
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$id = $_POST['post_id'];
if($email && $name && $comment){
//
$email = $db->real_escape_string($email);
$name = $db->real_escape_string($name);
$id = $db->real_escape_string($id);
$comment = $db->real_escape_string($comment);
if($addComment = $db->prepare("INSERT INTO comments(name, post_id, email_add, comment) VALUES (?,?,?,?)")){
$addComment->bind_param('ssss', $id, $name, $email, $comment);
$addComment->execute();
echo "Bedankt! uw bericht is toegevoegd";
$addComment->close();
} else{
echo "Error";
}
} else{
echo "ERROR";
}
}
?>
这是我页面的其余部分
<div id="container">
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
</div>
<?php endwhile;?>
</div>
</div>