php代码,用于在删除选定行时更新数据库中的其他行顺序
表名-新闻 列名-id、标题、发送\子\顶部、优先级 我将在新闻表中插入记录,并在优先级列中插入最大+1值,同时插入新记录 之后,从激活按钮更新send\u sub\u top=Active。 现在我需要的是,当我单击行时,我需要更新所选行列send\u sub\u top='',并更新优先级。 例如,如果数据库中有5行,其列send\u sub\u top=Active,优先级为1,2,3,4,5。 若我删除数据库中优先级为3的行,那个么其他行的优先级将更新为1,2,3,4。不设置为1,2,4,5。 请帮帮我 下面是我的代码。请告诉我如何更新数据库中的行优先级计数器php代码,用于在删除选定行时更新数据库中的其他行顺序,php,mysql,Php,Mysql,表名-新闻 列名-id、标题、发送\子\顶部、优先级 我将在新闻表中插入记录,并在优先级列中插入最大+1值,同时插入新记录 之后,从激活按钮更新send\u sub\u top=Active。 现在我需要的是,当我单击行时,我需要更新所选行列send\u sub\u top='',并更新优先级。 例如,如果数据库中有5行,其列send\u sub\u top=Active,优先级为1,2,3,4,5。 若我删除数据库中优先级为3的行,那个么其他行的优先级将更新为1,2,3,4。不设置为1,2,4
<!--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>
$('document').ready(函数(){
$('a')。单击(函数(){
var del_id=$(this.attr('remove_sub_top_news');
var parent=$(this.parent();
$.post('add_status_news.php',{remove_sub_top_news:del_id},函数(数据){
slideUp('slow',function(){$(this.remove();});
});
});
});
要在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下面的所有内容都不需要更改,但上面的所有内容都需要将值降低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的使用的?通过捕获价值和
一次性清理代码,使代码更干净,更易于阅读,
并且不太容易出现“缺少括号”错误李>
注意在查询中使用了“limit1”吗?帮助您的mysql服务器知道您只需要
一个结果,因此一旦找到一条记录,它就应该停止查找
您可以创建一个触发器
从副标题中删除其他新闻行吗?您只在更新其他行时遇到问题?是的。我只需要更新其他行优先级…一切正常。我只需要在删除和删除其他行时更新数据库中的其他行优先级。其他行将自动更新其优先级…正如@Daan建议的,我将为更新事件创建数据库触发器。