我在一个小项目中工作,在 excel 中读取一些数据文件,所以我将数据放入 MySQL 数据库中,然后在 php 文件中读取它,这样它就很容易读写,我将它放在分页页面中以便于在行之间传输trey 在 php 文件中制作一些代码作为我的一点经验,它工作正常并从数据库读取数据并更新它但是问题是当我按下更新按钮时它再次返回到第一页例如当我在页面中时4 然后按更新,他将数据发送到数据库,但他又将我返回到第一页<?phpinclude('db.php');if (isset($_GET['page_no']) && $_GET['page_no']!="") { $page_no = $_GET['page_no']; } else { $page_no = 1; } $total_records_per_page = 3; $offset = ($page_no-1) * $total_records_per_page; $previous_page = $page_no - 1; $next_page = $page_no + 1; $adjacents = "2"; $result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `b_2011`"); $total_records = mysqli_fetch_array($result_count); $total_records = $total_records['total_records']; $total_no_of_pages = ceil($total_records / $total_records_per_page); $second_last = $total_no_of_pages - 1; // total page minus 1 $result = mysqli_query($con,"SELECT * FROM `b_2011` LIMIT $offset, $total_records_per_page"); while($row = mysqli_fetch_array($result)){ echo"<form method=post action=index.php>"; echo "<tr> <td>"."<input type=text name=id value=".$row['id']." </td> <td>".$row['details_all']."</td> <td>"."<input type=text name=Class_send value=".$row['classfication']." </td> <td>" ."<input type=text name=det_send value=".$row['details']." </td> <td>" ."<input type=submit name=update value= update .</td> </tr>"; echo "</form>"; } if (isset($_POST['update'])){ $ID=$_POST['id']; $classsication=$_POST['Class_send']; $details=$_POST['det_send']; if (mysqli_query($con,"UPDATE b_2011 SET classfication='".$classsication."',details='".$details."' WHERE id=".$ID)) { echo "records updates"; } } ?>
1 回答
四季花海
TA贡献1811条经验 获得超5个赞
计算项目将显示的页面并在更新后重定向到该页面:
您已经从 获得了 id $ID=$_POST['id'];,让我们找到它的位置:
// Execute query, searching for id less or equal than the ID you've got previously
$result = mysqli_query($con,"SELECT COUNT(*) FROM `b_2011` WHERE id <= $ID");
// This query will return just 1 row with 1 value
$row = mysqli_fetch_row($result);
// First item holds the counter
$records = $row[0];
// Get page number to go
$page = ($records == 0) ? 1 : ceil($records / $total_records_per_page);
// Redirect to the page
header("Location: index.php?page_no=$page");
// End the script
exit;
顺便说一下,你的脚本有被攻击的风险,请考虑使用准备好的语句,你可以从我如何防止用 PHP 进行 SQL 注入?
- 1 回答
- 0 关注
- 209 浏览
添加回答
举报
0/150
提交
取消
