如果已经发布,如何限制用户不要再次在同一页面上发帖

时间:2016-09-18 10:02:07

标签: javascript php jquery mysql mysqli

我有以下用户评分和代码的代码工作正常的评论系统,但是用户可以一次又一次地发布和评价。

我希望如果用户发布了评论,他/她就不应该看到评论框,而是要显示“您已在此页面上发表评论”的消息。

我尝试使用Add-Comment.php中的查询,但没有效果。

需要帮助才能解决此问题。感谢

  

网址:index.php?id = 1

添加-Comment.php

<?php
session_start();
    $ipaddress = $_SERVER["REMOTE_ADDR"];
    $users     = session_id();

if(!empty($_POST)){
extract($_POST);
if($_POST['act'] == 'add-com'):
    $comment = htmlentities($comment);
    $rating = htmlentities($rating);

    // Connect to the database
    require_once '../../inc/db.php';

    $default = "mm";
    $size = 35;
    $grav_url = "http://www.gravatar.com/avatar/" . "?d=" . $default . "&s=" . $size;

$sql = "INSERT INTO rest_rating (rate, comment, sr_id, ip, user)
VALUES ('$rating', '$comment', '$id_post', '$ipaddress', '$users')";

$sqls = "select user from rest_rating
where sr_id = '$id_post' and user ='$users' )";
$tt =   $db->query($sqls);

    if ( $tt['user'] == $users ) {
    echo '<font size="3" color="red">You Have Already Rated For This Restaurant</font>';
    }elseif ( $db->query($sql)==true) {
?>

    <div class="cmt-cnt">
        <img src="<?php echo $grav_url; ?>" alt="" />
        <div class="thecom">
            <!--<h5><?php echo $name; ?></h5>-->
            <b>Rating : </b><?php echo $rating; ?>          
            <span  class="com-dt"><?php echo date('d-m-Y H:i'); ?></span>
            <br/>
            <p><?php echo $comment; ?></p>
        </div>
    </div><!-- end "cmt-cnt" -->

    <?php } ?>
<?php endif; ?>
<?php } ?>

的index.php

<?php
require_once '../../inc/db.php'; 
$id=$_GET['id'];
?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="css/example.css">
<link href="css/star-rating.css" media="all" rel="stylesheet" type="text/css"/>
<script src="js/star-rating.js" type="text/javascript"></script>

<div class="container">

<h3>Comments</h3>
<?php
$id_post = $id;
?> 
<div class="cmt-container" >
<?php
session_start();
$users   = session_id();
$results = $db->query("SELECT * FROM rest_rating WHERE sr_id = $id_post");
foreach ($results as $affcom) {
$comment = $affcom['comment'];
$rating = $affcom['rate'];
$date = $affcom['date'];
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/" . "?d=" . $default . "&s=" . $size;
?>

<div class="cmt-cnt">
<div class="thecom">
<input id="input-5a" class="rating" value="<?php echo $rating; ?>" data-size="xs" data-show-clear="false" data-show-caption="false" data-readonly="true">
<span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
</div>
</div><!-- end "cmt-cnt" -->
<?php } ?>



<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input name="starrating" id="starrating" value="1" type="number" class="rating" min=0 max=5 step=1 data-size="xs2" >
<textarea class="the-new-com"></textarea>

<div class="bt-add-com">Post comment</div>
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->

<?php
$sqls = "select user from rest_rating
where sr_id = '$id_post' and user ='$users' )";
$tt=$db->query($sqls);

$userT=$tt['user'];
?>
<script type="text/javascript">
   $(function(){ 
//alert(event.timeStamp);
$('.new-com-bt').click(function(event){
$(this).hide();
$('.new-com-cnt').show();
$('#name-com').focus();
});

/* when start writing the comment activate the "add" button */
$('.the-new-com').bind('input propertychange', function() {
   $(".bt-add-com").css({opacity:0.6});
   var checklength = $(this).val().length;
   if(checklength){ $(".bt-add-com").css({opacity:1}); }
});

/* on clic  on the cancel button */
$('.bt-cancel-com').click(function(){
$('.the-new-com').val('');
$('.new-com-cnt').fadeOut('fast', function(){
$('.new-com-bt').fadeIn('fast');
});
});

// on post comment click 
$('.bt-add-com').click(function(){
var theCom = $('.the-new-com');
var starrating = $('#starrating');

if( !theCom.val()){ 
alert('You need to write a comment!'); 
}else{
$.ajax({
type: "POST",
url: "add-comment.php",
data: 'act=add-com&id_post='+<?php echo $id_post; ?>+'&rating='+starrating.val()+'&comment='+theCom.val(),
success: function(html){
theCom.val('');
starrating.val('');
$('.new-com-cnt').hide('fast', function(){
$('.new-com-bt').show('fast');
$('.new-com-bt').before(html);  
})
}
});
}
});

});
</script> 

</div>

1 个答案:

答案 0 :(得分:3)

您可以构建查询以检查用户是否已在 特定页面上发布评论,并相应地显示评级和评论框。这是代码片段,

// your code

$results = $db->query("SELECT * FROM rest_rating WHERE sr_id = '". $id_post . "' AND user ='". $users . "'");
if($results->num_rows){
    // user has already posted a comment 
    echo '<p>You have already posted comment on this page</p>';
}else{
    // user hasn't posted any comment on this page yet
    // display rating and comment box
    ?>
    <div class="new-com-bt">
    <span>Write a comment ...</span>
    </div>
    <div class="new-com-cnt">
    <input name="starrating" id="starrating" value="1" type="number" class="rating" min=0 max=5 step=1 data-size="xs2" >
    <textarea class="the-new-com"></textarea>

    <div class="bt-add-com">Post comment</div>
    <div class="bt-cancel-com">Cancel</div>
    </div>
    <div class="clear"></div>
    </div><!-- end of comments container "cmt-container" -->
    <?php
}

// your code
相关问题