2016-09-18 7 views
0

私は以下のユーザー評価用のコードを持っています&コメントシステムは正常に動作していますが、既に投稿されている場合に同じページに再度投稿しないようにユーザーを制限する方法

ユーザーが既にコメントを投稿した場合は、コメントボックスは表示しないでください。「既にこのページにコメントを投稿しました」というメッセージが表示されます。

Add-Comment.phpのクエリを使用しようとしましたが、機能しませんでした。

この問題を解決するにはヘルプが必要です。おかげ

URL:?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> 

答えて

3

することができますwheを確認するクエリを作成するユーザはすでににコメントを投稿しており、そのページはであり、それに応じて評価とコメントボックスを表示します。コードスニペットは次のとおりです。

// 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 
関連する問題