-1

i work in small project of reading some data files in excel so i put the data in MySQL data base then read it in php files so it was easy to read and write on it , i make it in pagination pages to easy transfer between rows i trey to make some code in php files as my little experience on it , it work fine and read data from data base and also update it but problem was when i press update button it return me again to first page for example when i was in page 4 and press update he send the data to data base but he return me again to first page

<?php
include('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";
 }

 
}

 
    ?>
  • 3
    Don't you have a more clear code? Without html and php mixing like that, this is pretty hard to read and understand. However, the fact it redirects to first page when submitting is normal, when you submit your data, it only sends input form fields (`id`, `Class_send` and `det_send`). The page number you received through a GET request is not part of the form submission. You may add it as an hidden field. – Pierre Apr 08 '20 at 15:40
  • i edit it now see it – ibrahim mahmoud Apr 08 '20 at 15:43

1 Answers1

0

Calculate the page where the item will show and redirect to that page after updating:

You've already got the id from $ID=$_POST['id'];, lets find it's position:

// 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;

By the way, your scripts are at risk of attack, please considere using prepared statements, you can start with How can I prevent SQL injection with PHP?

Triby
  • 1,739
  • 1
  • 16
  • 18