表名 - 新闻 列名 - id,标题,send_sub_top,优先级。
我在新闻表中插入记录,并且在优先级列中插入max + 1值并插入新记录。
之后从激活按钮更新send_sub_top = Active。
现在我需要的是当我点击行时我需要更新选定的行列send_sub_top =''并且还会更新优先级。
例如 - 如果我在数据库中有5行,其列send_sub_top = Active且优先级如1,2,3,4,5。
然后如果我在数据库中删除优先级为3的行,则其他行优先级将更新为1,2,3,4。不像1,2,4,5那样设置。
请帮帮我
下面是我的代码。 plz建议我如何更新数据库中的行优先级计数器
<!--script to update selected news from SUB TOP section-->
<script type="text/javascript">
$('document').ready(function(){
$('a').click(function(){
var del_id = $(this).attr('remove_sub_top_news');
var parent = $(this).parent();
$.post('add_status_news.php', {remove_sub_top_news:del_id}, function(data){
parent.slideUp('slow', function() {$(this).remove();});
});
});
});
</script>
<!--END-->
<div style="border:1px solid #000; float:left; width:400px; padding:5px 4px 5px 4px; height:225px">
<div id="contentLeft1">
<ul>
<?php
foreach($sub_top_select as $sub_top) {
?>
<li id="recordsArray1_<?php echo $sub_top['id']; ?>"><a href="javascript:return(0);" remove_sub_top_news="<?php echo $sub_top['id']; ?>">
<img src="img/error.png" height="14px" width="14px" /></a> <?php echo $sub_top['headline']; ?></li>
<?php } ?>
</ul>
</div>
</div>
要在add_status_news.php页面上更新的php代码
<?php
if(isset($_POST['remove_sub_top_news'])) {
$id = mysql_real_escape_string(trim(htmlspecialchars($_POST['remove_sub_top_news'])));
$sql = "SELECT id,send_sub_top FROM news WHERE id='$id'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
mysql_query("Update news SET send_sub_top='' WHERE id=".mysql_real_escape_string(trim(htmlspecialchars($_POST['remove_sub_top_news']))));
if(mysql_affected_rows() > 0) {
$_SESSION['message'] = "News Removed From SubTop Successfully";
header("Location:sethomepage.php");
exit;
}
}
?>
答案 0 :(得分:0)
您需要了解的是问题的本质......以及解决方案。 首先,您需要让数据库知道已删除条目的ID。由于数字的性质(根据您的描述),删除的ID下面的所有内容都不需要更改,但上面的所有内容都需要按值减少1
因此,如果我理解正确,您的代码需要如下所示:
/*sanitize AND set the $id in one pass*/
if($id = intval($_POST['remove_sub_top_news'])) {
$sql = "SELECT id,send_sub_top FROM news WHERE id='$id' limit 1";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
//get the priority of the current news item
$target_priority = $row['priority'];
mysql_query("Update news SET send_sub_top='' WHERE id=$id limit 1");
if(mysql_affected_rows() > 0) {
$_SESSION['message'] = "News Removed From SubTop Successfully";
/*
update priorities
WHAT THIS IS DOING:
Using the priority of the currently item (the one you just "send_sub_top=''" on... as a 'cut off point' update all news items that have a priority higher than that item.
So if it was priority 5, for example, your priorities are now 1,2,3,4,6, 7, etc etc. So the query will update 6 and 7 and above by lowering them by 1, each...leaving you with 1,2,3,4,5 and 6.
As this is repetitive, you could, as suggested above, use a trigger, but the logic is sound.
*/
mysql_query("Update news SET priority=priority-1 WHERE priority > $target_priority");
header("Location:sethomepage.php");
exit;
}
}
注意: