【问题标题】:I have a comment form in fullpost.php and the inserting form is in sql.php, how do I get post_id for my comments table?我在 fullpost.php 中有一个评论表单,而插入表单在 sql.php 中,如何获取我的评论表的 post_id?
【发布时间】:2017-08-18 02:18:21
【问题描述】:

我在 fullpost 页面中有我的评论表单,我正在使用 ajax 处理表单,但我不知道如何在我的 cmets 表中插入 post_id 以指定帖子评论。请帮助 当我试图在 sql.php 中获取 get_id 时,我得到一个错误,显示 undefined post_id in line 13 sql.php

请在下面找到我的代码:

fullpost.php

?php 
ob_start();
require_once('includes/header.php'); ?>
<?php

$db = new Database();

if(isset($_GET['id'])){

    $id = $_GET['id'];

    $query = "SELECT * FROM posts WHERE post_id ='$id'";
    $post = $db->select($query);
}


$c_query = "SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC";
$c_run = $db->select($c_query);

?>
    <div class ="col-8">
        <div class="content-area">
           <?php if($post):?>
                    <?php while($row = $post->fetch_array()):?>
            <h2 class="title-full-post"><?php echo $row['title'];?></h2>
            <p class="content-area-auth"><?php echo formatDate($row['created_date']);?> By
            <a class="content-area-author" href=""><?php echo $row['author']; ?></a></p>
            <img class="post-img" src="assets/images/<?php echo $row['image']; ?>">
            <p class="content-area-body"><?php echo $row['body'];?><br /><br /><br />
            </p>
            <?php endwhile; ?>  
                <?php endif; ?> 
            <div class="comment">
                <h2>Recent Comments</h2>
            <?php

                if($c_run):
                    while($r = $c_run->fetch_assoc()):
            ?>
             <p><?php echo $r['name']; echo $r['date']; ?></p>
             <p><?php echo $r['comment']; ?></p>
             <!--<input type='button' name='reply' id='reply' value='Reply' onclick='replyComment("<?php echo $message_id?>")' />-->

             <hr>
             <?php
                endwhile;
                endif;
                ?>

            </div>

            <div class="comments-area" id="editbutton">
                <h2>Comment Below</h2>

                <form action="fullpost" id ="commentForm" method="post" class="form-commment">
                    <input type="hidden" name="commentId" value="<?php echo $id ?>">
                    <label for="">Name</label><span style="color: red;">*</span>
                    <input type="text" name="name" id="commentName" class="form-control-comment" placeholder="Type your Name..">
                    <label for="">Email</label>
                    <input type="Email" name="email" id="commentEmail" class="form-control-comment" placeholder="Type your Email..">
                    <label for="">Website</label>
                    <input type="text" name="website" id="commentWebsite" class="form-control-comment" placeholder="Type your Website..">
                    <label for="">Comment</label>
                    <textarea name="comment" id="commentMessage" class="form-control-comment form-text-area">Comment ...</textarea>
                    <input type="submit" id="submitComment" name="submitComment" class="btn-comment" value="Post Comment">
                    <span id="errorMessage"></span> 
                    <span id="successMessage"></span>   
                </form>
            </div>      
<script type="text/javascript">
    $(document).ready(function(){
        $("#commentForm").submit(function(event){
            event.preventDefault();

            var commentName = $("#commentName").val();
            var commentEmail = $("#commentEmail").val();
            var commentWebsite = $("#commentWebsite").val();
            var commentMessage = $("#commentMessage").val();

            if(commentName == '' || commentEmail == '' || commentMessage == '' ){
                $("#errorMessage").html("Fill all the required fields");
            } else{
                $("#errorMessage").html('');

            $.ajax({
                url: "sql.php",
                type: "POST",
                async: false,
                data:{
                    "done": 1,
                    "username": commentName,
                    "useremail": commentEmail,
                    "userwebsite": commentWebsite,
                    "usercomment": commentMessage,
                    "commentId": commentId
                },
                success: function(data){
                    //$("#name").val('');
                    //$("#message").val('');
                    $("form").trigger("reset");
                    $("#successMessage").fadeIn().html(data);
                }

            });
           }
        });
    });
</script>
        </div><!--End of Content Area-->
    </div><!--end of col-8-->   

<?php require_once('includes/footer.php'); ?>



<!--- this is sql.php-->
<?php
      require_once('config/config.php');
      require_once('libraries/database.php');
      require_once('helpers/format_helpers.php');
//require_once('includes/header.php');
//$conn= new mysqli('localhost','root','','myblog');
$db = new Database();

//$id = $_GET['id'];
/*Insert comments*/
if(isset($_POST['done'])){

    $name = mysqli_real_escape_string($db->link, $_POST['username']);
    $email = mysqli_real_escape_string($db->link, $_POST['useremail']);
    $website = mysqli_real_escape_string($db->link, $_POST['userwebsite']);
    $comment = mysqli_real_escape_string($db->link, $_POST['usercomment']);
    $postId = mysqli_real_escape_string($db->link, $_POST
        ['commentId']);

    if(!empty($name && $email && $comment)){

      // $conn= new mysqli('localhost','root','','myblog');
        $i_query ="INSERT INTO comments(name, email, website, comment, post_id)VALUES('$name','$email','$website','$comment','$postId')";
        $insert_query =$db->insert($i_query);
        if($insert_query){
            echo "Comment has been submitted and waiting for approval..";

        } else{
            $error_message ="Please try again..comment not submitted";
        }

    } else{
        $error_message ="All(*)Fields Are required";
    }
}

任何帮助将不胜感激

【问题讨论】:

  • 您的代码对 SQL 注入:post_id ='$id' 两次开放,JavaScript 注入:&lt;?php echo $id ?&gt;$id 永远不会被转义。
  • connect(); } 私有函数 connect(){ $this->link = new mysqli($this->host, $this->user, $this->password, $this->dbName); if(!$this->link){ $this->error="连接失败..与数据库建立连接时遇到错误".$this->link->connect_error;返回假; } }
  • 在代码中看不到任何名为 post_id 的变量。
  • 请修正问题的标题...
  • 你好 ochi,我正在用 php 制作博客,我已经创建了一个评论框,用于使用完整的 post.php 和 sql.php 中的插入查询页面的博客文章,我担心的是当我在我的数据库表 cmets 中发布评论 m 时没有得到 post_id 时,我如何插入特定帖子的 post_id,代码在上面给出,请帮助..

标签: javascript php mysql ajax html


【解决方案1】:

您可以使用mysql_insert_id() 返回我的 MySQL 生成的最后一个 ID。

如果你展示你的 Database 课程,我可以给你一个例子。

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 2014-08-12
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多