gpt4 book ai didi

javascript - 我在 fullpost.php 中有一个评论表单,而插入表单在 sql.php 中,如何获取评论表的 post_id ?

转载 作者:行者123 更新时间:2023-11-29 18:31:00 24 4
gpt4 key购买 nike

我的评论表单位于 fullpost 页面中,并且正在使用 ajax 处理表单,但我不知道如何在评论表中插入 post_id 来指定帖子评论。请帮忙因为我试图在 sql.php 中获取 get_id 但出现错误,该错误显示第 13 行 sql.php 中的 undefined post_id

请在下面找到我的代码:

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";
}
}

任何帮助将不胜感激

最佳答案

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

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

关于javascript - 我在 fullpost.php 中有一个评论表单,而插入表单在 sql.php 中,如何获取评论表的 post_id ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45698265/

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