表名-新闻 列名 - id,headline,send_sub_top,priority.
我正在在新闻表中插入记录,同时在插入新记录的同时在优先级列中插入 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 那样设置。
请帮帮我
下面是我的代码。请建议我如何更新数据库中的行优先级优先级计数器
<!--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;
}
}
?>
最佳答案
您需要了解问题的性质……以及解决方案。 首先,您需要让您的数据库知道已删除条目的 ID。由于数字的性质(根据您的描述),已删除 ID 以下的所有内容都不需要更改,但以上所有内容的值(value)都需要降低 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;
}
}
注意事项:
- 请注意我是如何简化您对 $id 的使用的?通过捕获值(value)和 一次清理它,你让你的代码更干净,更容易阅读, 并且不太容易出现“缺少括号”错误。
- 注意到在查询中使用了“limit 1”吗?帮助你的 mysql 服务器知道你只需要 一个结果,因此一旦找到一条记录,它就应该停止查找。
关于删除选定行时更新数据库中其他行顺序的 PHP 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486880/