gpt4 book ai didi

php ajax评论系统不起作用查询不插入任何内容

转载 作者:行者123 更新时间:2023-11-29 21:16:10 24 4
gpt4 key购买 nike

我的评论系统使用以下代码当我输入评论和名称并单击提交按钮时,我什么也没得到没有错误或警告没有插入任何内容有人可以找到代码有什么问题

index.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
function sendPost() {
var comment = document.getElementById("comment").value;
var name = document.getElementById("username").value;
if (comment && name) {
$.ajax
({
type: 'POST',
url: 'post_comment.php',
data: {
user_comm: comment,
user_name: name
},
success: function (response) {
console.log(response);
document.getElementById("all_comments").innerHTML = response + document.getElementById("all_comments").innerHTML;
document.getElementById("comment").value = "";
document.getElementById("username").value = "";
}
});
}
return false;
}
</script>
</head>
<body>
<form method='post' action="" onsubmit="return sendPost();">
<textarea id="comment" name="comment" placeholder="Write Your Comment Here....."></textarea>
<input type="text" id="username" name="username" placeholder="Your Name">
<input type="submit" value="Post Comment">
</form>
<div align="center" id="all_comments">
<?php
include_once 'dbConfig.php';
$comm = $connect->query("select name,comment,post_time from comments order by post_time desc");
while($row=$comm->fetch_array(MYSQLI_ASSOC))
{
$name=$row['name'];
$comment=$row['comment'];
$time=$row['post_time'];
?>
<div class="comment_div">
<p class="name">Posted By:<?php echo $name;?></p>
<p class="comment"><?php echo $comment;?></p>
<p class="time"><?php echo $time;?></p>
</div>
<?php
}
?>
</div>
</body>
</html>

post_comment.php

<?php
include_once 'dbConfig.php';
if(isset($_POST['user_comm']) && isset($_POST['user_name']))
{
$comment=$_POST['user_comm'];
$name=$_POST['user_name'];
$insert=$connect->query("insert into comments values('','$name','$comment',CURRENT_TIMESTAMP)");
$id= $connect->insert_id;
$select=$connect->query("select name,comment,post_time from comments where name='$name' and comment='$comment' and id='$id'");
if($row=$select->fetch_array(MYSQLI_ASSOC))
{
$name=$row['name'];
$comment=$row['comment'];
$time=$row['post_time'];
?>
<div class="comment_div">
<p class="name">Posted By:<?php echo $name;?></p>
<p class="comment"><?php echo $comment;?></p>
<p class="time"><?php echo $time;?></p>
</div>
<?php
}
exit;
}
else{
echo "No Data Is set";
}

?>

最佳答案

尝试使用

data: {
"user_comm": comment,
"user_name": name
}

并且您的插入查询缺少列名称:

/* JUST REPLACE THE NECESSARY COLUMN NAME */
$insert = $connect->query("INSERT INTO comments (column1, column2, column3, column4) VALUES ('','$name','$comment',CURRENT_TIMESTAMP)");

要进一步排查问题,请尝试在 success: function(response){} 中使用 console.log('success'); 显示任何数据。如果它没有显示success消息,那么您的post_comment.php就有问题。

关于php ajax评论系统不起作用查询不插入任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931592/

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