[英]UPDATE all rows in while loop
I'm trying to wrap my brain around what I'm doing wrong here. 我试图把我的大脑包裹在我在这里做错的事情上。 This is the function that is called when I want to delete a record.
这是我想要删除记录时调用的函数。 The record is deleted, but I want the display_order to reestablish itself, if the record that is delete is somewhere in the middle of the rows.
记录被删除,但如果删除的记录位于行中间的某个位置,我希望display_order重新建立自己。 So that the first record will always be 1, the second always 2, etc.
这样第一条记录总是1,第二条记录总是2,等等。
<?php
$counter = 1;
require ("connection.php");
$sql = "SELECT * FROM pages";
$result = $conn->query($sql) or die(mysqli_error());
$id = $_GET['id'];
$delete = mysqli_query($conn, "DELETE FROM pages WHERE id ='".$id."'");
if ($delete){
while($row = $result->fetch_object()){
$reord = "UPDATE pages SET display_order = '".$counter."'";
$result2 = $conn->query($reord) or die(mysqli_error());
$counter++;
}
header("Location: admin.php");
}else{
echo "NOTHING DELETED id=".$id;
}
?>
The error that I'm receiving is that all the records "display_order" are now updated to the maximum number of records, say for instance, I had four records, I deleted one of them, now all remaining records have a display_order of 3. 我收到的错误是所有记录“display_order”现在都更新为最大记录数,例如,我有四条记录,我删除了其中一条,现在所有剩余记录的display_order为3。
You missed to add the page id as a WHERE condition to the query. 您错过了将页面ID作为WHERE条件添加到查询中。 Like this:
像这样:
while($row = $result->fetch_object()){
$reord = "UPDATE pages SET display_order = '".$counter."' WHERE page_id = '" . $row->page_id . "'";
$result2 = $conn->query($reord) or die(mysqli_error());
$counter++;
}
Note: If missing the WHERE clause all records in the pages
table will be updated. 注意:如果缺少WHERE子句,则会更新
pages
表中的所有记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.