-1

您好,我是 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>
4

2 回答 2

1

您的表单中没有名称为 post_id 的字段。但是,您通过表单操作中的 URL 手动传递 ID。要获取 ID,您将使用$_GET['id']而不是$_POST['post_id']

于 2013-07-25T23:31:54.193 回答
0

变量 post_id 未设置(无论如何在 post 中)。我会改变这些

$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$id = $_POST['post_id'];

类似于

$email = !empty($_POST['email']) ? $_POST['email'] : '';
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$comment = !empty($_POST['comment']) ? $_POST['comment'] : '';
$id = !empty($_POST['post_id']) ? $_POST['post_id'] : '';

这样,如果表格没有完全填写,您就有一个备用值。

于 2013-07-25T23:30:16.617 回答