将数据插入数据库不起作用


Inserting data into database not working

我正在用PHP/MySQL写简单的博客,我在数据库中插入一些数据时遇到了问题。我正在尝试添加评论总是收到错误 - 未添加评论。我无法弄清楚代码出了什么问题。有人能帮忙吗?

<?php 
if(!isset($_GET['id'])) {
header('Location: index.php');
exit();
} else {
$id = $_GET['id'];
}
if(!is_numeric($id)) {
header('Location: index.php');
}
// Include database connection
include('includes/db_connect.php');
$sql = "SELECT post_title, post_body FROM posts WHERE post_id='$id'";
$query = $db->query($sql);
//echo $query->num_rows;
if($query->num_rows != 1) {
header('Location: index.php');
exit();
}
if(isset($_POST['submit-comment'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$email = $db->real_escape_string($email);
$name = $db->real_escape_string($name);
$comment = $db->real_escape_string($comment);
$id = $db->real_escape_string($id);
if($email && $name && $comment) {
    $sqlComment = "INSERT INTO comments (post_id, email, name, comment) VALUES ('$id','$email','$name','$comment')";
    $queryComment = $db->query($sqlComment);
    if($queryComment) {
        echo "Comment was added";
    } else {
        echo "Comment not added";
    }
} else {
    echo "Error";
}
}
?>
<! DOCTYPE html >
<!--[if lt IE 7]>  <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>     <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>     <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--><html class=""><!--<![endif]-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blog System</title>
<link rel="stylesheet" href="css/application.css" type="text/css">
 <style type="text/css">
    label {
        display: block;
    }
</style>
</head>
<body>
<div id="container">
<div id="post">
    <?php 
    $row = $query->fetch_object();
    echo "<h2>" . $row->post_title . "</h2>";
    echo "<p>" . $row->post_body . "</p>";
    ?>
</div>
<hr>
<div id="add-comments">
    <form action="<?php echo $_SERVER['PHP_SELF'] . '?id=' . $id ?>" method="post">
        <label for="email">Email Address:</label>
        <input type="text" name="email" id="email"><br>
        <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
        <label for="comment">Comment</label>
        <textarea name="comment" id="comment" cols="30" rows="10"></textarea><br>
        <br><br>
        <input type="submit" name="submit-comment" value="Post your comment" id="postyourcomment">
    </form>
</div>
</div>
<script type="text/javascript" src="js/application.min.js"></script>
</body>
</html>
<?php 
if(isset($_POST['submit-comment'])) {
    if(!isset($_GET['id'])) {
        header('Location: index.php');
        exit();
    } else {
        $id = $_GET['id'];
    }
    if(!is_numeric($id)) {
        header('Location: index.php');
    }
    // Include database connection
    include('db_connect.php');
    $sql = "SELECT post_title, post_body FROM posts WHERE post_id=".$id." ";
    $query = $db->query($sql);
    //echo $query->num_rows;
    if($query->num_rows != 1) {
        header('Location: index.php');
    exit();
    }
    $email = $_POST['email'];
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    $email = $db->real_escape_string($email);
    $name = $db->real_escape_string($name);
    $comment = $db->real_escape_string($comment);
    $id = $db->real_escape_string($id);
    if($email && $name && $comment) {
        $sqlComment = "INSERT INTO comments (post_id, email, name, comment) VALUES (".$id.",'".$email."','".$name."','".$comment."')";
        $queryComment = $db->query($sqlComment);
        if($queryComment) {
            echo "Comment was added";
        } else {
            echo "Comment not added";
        }
    } else {
        echo "Error";
    }
}
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blog System</title>
<link rel="stylesheet" href="file:///C|/Users/Jaydeep Jivani/Desktop/css/application.css" type="text/css">
<style type="text/css">
    label {
        display: block;
    }
</style>
</head>
<body>
<div id="container">
<div id="post">
<?php 
$row = $query->fetch_object();
echo "<h2>" . $row->post_title . "</h2>";
echo "<p>" . $row->post_body . "</p>";
?>
</div>
<hr>
<div id="add-comments">
    <form action=<?=$_SERVER['PHP_SELF']?> method="get">
        <input type="hidden" name="id" value=<?=$id?> />
        <label for="email">Email Address:</label>
        <input type="text" name="email" id="email"><br>
        <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
        <label for="comment">Comment</label>
        <textarea name="comment" id="comment" cols="30" rows="10"></textarea><br>
        <br><br>
        <input type="submit" name="submit-comment" value="Post your comment" id="postyourcomment">
    </form>
</div>
</div>
<script type="text/javascript" src="file:///C|/Users/Jaydeep Jivani/Desktop/js/application.min.js"></script>
</body>
</html>

谢谢大家的帮助。我发现了一个与我的数据库有关的问题,不幸的是我用comment_id构建了表格,忘记添加AI属性。

多亏了@tadman我才能重写我的代码,这是最终的工作结果:

if(isset($_POST['submit-comment'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$email = $db->real_escape_string($email);
$name = $db->real_escape_string($name);
$comment = $db->real_escape_string($comment);
$id = $db->real_escape_string($id);
if($email && $name && $comment) {
    // Prepare statemnt
    $sqlComment = "INSERT INTO comments (post_id, email, name, comment) VALUES (?, ?, ?, ?)";
    $queryComment = $db->prepare($sqlComment);
    $queryComment->bind_param('ssss', $id, $email, $name, $comment);
    // Execute prepared statement
    $queryComment->execute();
    if($queryComment) {
        echo "Comment was added.";
    } else {
        echo "There was a problem. Error: " . mysqli_error($db);
    }
    // Close statement
    $queryComment->close();
} else {
    echo "Error";
}