我使用这个jquery脚本将数据发送到数据库:
$(document).ready(function(){
$(".commentForm").bind('submit', function(){
var usercomment = $(".articleComment").val();
var categoryid = $(".categoryid").val();
var userid = $(".userid").val();
var ARID = $(".ARID").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'userid='+ userid + '&articleComment='+ usercomment + '&ARID='+ ARID + '&categoryid='+ categoryid;
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
cache: false,
success: function(response){
$(".articlebox").html(response);
$(".commentForm").trigger("reset");
}
});
return false;
});
});
这是一个while语句之后的表单位置,它为一个给定的类别生成所有文章。
<form class="commentForm" method="POST" action="comment.php">
<textarea class="articleComment" placeholder="Leave a comment..." name='articleComment'></textarea>
<br><br>
<?php if ($myuserplan['Plan_name'] == 'Free'): ?>
<button disabled type="submit" class="btn btn-default pull-right" name='addComment'>Send</button>
<?php elseif ($myuserplan['Plan_name'] != 'Free'): ?>
<button type="submit" class="btn btn-default pull-right submitme" name='addComment'>Send</button>
<?php endif; ?>
<input class="userid" type='hidden' value='<?= $loginid_session; ?>' name='userid'>
<input class="ARID" type='hidden' value='<?= $getarticle['ARID']; ?>' name='ARID'>
<input class="categoryid" type='hidden' value='<?= $getarticle['CategoryId']; ?>' name='categoryid'>
</form>
每个类别只有一篇文章,一切正常。当同一页面上有多篇文章时,当我评论一篇文章时,同一类别中的所有其他文章也会收到错误的评论。我尝试了.live
和.submit
相同的行为。我该如何解决这个问题?
编辑comment.php
//get variables
$articleComment = $_POST['articleComment'];
$ARID = $_POST['ARID'];
$userid = $_POST['userid'];
$categoryid=$_POST['categoryid'];
$id = $categoryid;
//insert in ve_articles_comments
$usercommentQ = $db->prepare("INSERT INTO ve_articles_comments (UserId,ArticleId,Comments) VALUES (?,?,?)");
$usercommentQ->bind_param("iis",$userid,$ARID,$articleComment);
$usercommentQ->execute() or die(mysqli_error($db));
$selectarticleN= mysqli_query($db, "SELECT ArticleName, CategoryId FROM ve_articles WHERE ARID='$ARID'");
$rowart = mysqli_fetch_array($selectarticleN,MYSQLI_ASSOC);
$article_name = $rowart['ArticleName'];
$article_cat = $rowart['CategoryId'];
$selectarticleQ= mysqli_query($db, "SELECT Category FROM ve_articles_categories WHERE id='$article_cat'");
$rowcat = mysqli_fetch_array($selectarticleQ,MYSQLI_ASSOC);
$category_name = $rowcat['Category'];
$event="commented on $article_name in $category_name";
$livestreamQ = $db->prepare("INSERT INTO ve_livestream (UserId,Event) VALUES (?,?)");
$livestreamQ->bind_param("is",$userid,$event);
$livestreamQ->execute() or die(mysqli_error($db));
$getuserarticles = "SELECT * FROM ve_articles a LEFT JOIN ve_articles_categories c ON a.CategoryId=c.id LEFT JOIN ve_users u ON a.UsersId=u.UserID WHERE a.CategoryId='$id' GROUP BY a.ARID ORDER BY a.ArticleTime";
$resultarticles =mysqli_query($db,$getuserarticles);
$getarticle = mysqli_fetch_array($resultarticles,MYSQLI_ASSOC);
$articleCommsQ="SELECT * FROM ve_articles_comments ac JOIN ve_users u ON ac.UserId=u.UserID WHERE ac.ArticleId={$getarticle['ARID']} ORDER BY DateTime DESC";
$resultarticlecomms=mysqli_query($db, $articleCommsQ);
if ($usercommentQ) {
while ($getarticlecomms = mysqli_fetch_array($resultarticlecomms)) {
$checkavatar = mysqli_query(
$db,
"SELECT * FROM ve_photos p WHERE p.UserID={$getarticle['UsersId']} AND IsPrimaryPhoto='1' ORDER BY InsertDate LIMIT 1"
);
$rowav = mysqli_fetch_array($checkavatar, MYSQLI_ASSOC);
echo " <div class='row'> ";
echo "<div style='width:75% !important; margin:auto;' class='articlebox'>";
echo " <p style=''> ";
echo " <a href='profile.php?viewuserprofile= " . $getarticle['UsersId'] . ">' ";
if (mysqli_num_rows($checkavatar) == 0) {
echo " <img style='width: 10% !important;' class='tableimg' src='uploads/userdefault.png' alt='profilename'> ";
} elseif (mysqli_num_rows($checkavatar) > 0) {
echo " <img style='width: 10% !important;' class='tableimg' src='core/" . $rowav['PhotoPath'] . " >' alt='profilename'> ";
}
echo " </a> ";
echo " </p> ";
echo " <span style='padding-left:10%;'>" . $getarticlecomms['ProfileName'] . " | " . $getarticlecomms['DateTime'] . " </span>";
echo " <p class='bubbleother pull-right'>" . $getarticlecomms['Comments'] . " </p> ";
echo " </div> ";
echo " </div> ";
}
}
$usercommentQ->close();
$db->close();