如果已经发布,如何限制用户不要在同一页面上再次发布
Posted
技术标签:
【中文标题】如果已经发布,如何限制用户不要在同一页面上再次发布【英文标题】:how to restrict the user not to post again on the same page if already posted 【发布时间】:2017-01-26 03:41:44 【问题描述】:我有下面的用户评分和评论系统代码,它工作正常,但用户可以一次又一次地发布和评分。
我希望如果用户已经发表了评论,他/她不应该看到评论框,而是显示“您已经在此页面上发表评论”的消息。
我尝试使用Add-Comment.php
中的查询,但没有奏效。
需要帮助来解决此问题。谢谢
网址:index.php?id=1
Add-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; ?>" />
<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】:您可以构造一个查询来检查用户是否已经在那个特定页面上发表了评论,并相应地显示评级和评论框。这是代码sn-p,
// 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
【讨论】:
以上是关于如果已经发布,如何限制用户不要在同一页面上再次发布的主要内容,如果未能解决你的问题,请参考以下文章
服务器重启后,再次访问arcgis服务,页面弹出输入arcgis用户名和密码的框框