Jump to content

PHP Mysql records not being updated on first page load - Why?


bambinou1980

Recommended Posts

Hello,

 

I am having a strange problem, I created a "customers delete" page and a "customer list" page, when I delete a record, it is removed from mysql but when I click on "list-customers" once, the record is still showing, I need to reload the page again to get the record to be gone, it is weird....Any idea what could cause this please? It looks like the records are cached some how but I have not added any cache addons to my script, it is a bare script with nothing else than pure php, mysql and bootstrap.

 

Here is my deletion page:

 

 

 



<?php
ob_start();
session_start();
$admin_permission = $_SESSION['admin_permission'];
if(($admin_permission) == 1){
//Session admin ID equal 1
}else{
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/index.php');
exit();
}
?>
<?php include($_SERVER["DOCUMENT_ROOT"] . "/admin/includes/admin-header.php"); ?>


<!--Content-->

<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">


<?php
$customer_id = $_GET['customer'];
if(!$customer_id){
echo "<div class='alert alert-danger' role='alert'>Customer not found!</div>";
}else{

$query = "DELETE FROM customers WHERE id = $customer_id";
$result = mysqli_query($connection, $query);
}

if ($result && mysqli_affected_rows($connection) == 1) {
// Success
// redirect_to("somepage.php");
echo "<div class='alert alert-success' role='alert'>Record Deleted!</div>";
} else {
// Failure
// $message = "Subject update failed";
die("Database query failed. " . mysqli_error($connection));
}


?>



</div>

</div>
</div>
</div>
</div>

<!--Content-->
<?php
mysqli_close($connection);
ob_flush();
?>
<?php include($_SERVER["DOCUMENT_ROOT"] . "/admin/includes/admin-footer.php"); ?>


 

 

 

And here my customer listing page:

 

 



<?php
ob_start();
session_start();
$admin_permission = $_SESSION['admin_permission'];
if(($admin_permission) == 1){
//Session admin ID equal 1
}else{
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/index.php');
exit();
}
?>
<?php include($_SERVER["DOCUMENT_ROOT"] . "/admin/includes/admin-header.php"); ?>

<!--Content-->

<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<h3>
List of customers records
</h3>

<?php
// This first query is just to get the total count of rows
$sql = "SELECT COUNT(id) FROM customers";
$query = mysqli_query($connection, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 5;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
}
if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT id, cust_company, cust_name, cust_surname, cust_address, cust_phone, cust_email, cust_vat FROM customers ORDER BY id DESC $limit";
$query = mysqli_query($connection, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Testimonials (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a>     ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum - 4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>   ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.'   ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum + 1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>   ';
if($i >= $pagenum + 4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= '     <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
}
}
$list = '';
?>



<ul class="pager">
<li class="previous"><?php echo $paginationCtrls; ?></li>
</ul>

<?php
// Attempt select query execution
if($result = mysqli_query($connection, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th class='col-md-1'>id</th>";
echo "<th class='col-md-2'>Company</th>";
echo "<th class='col-md-1'>Name</th>";
echo "<th class='col-md-1'>Surname</th>";
echo "<th class='col-md-2'>Address</th>";
echo "<th class='col-md-1'>Phone</th>";
echo "<th class='col-md-2'>Email</th>";
echo "<th class='col-md-2'>V.A.T</th>";
echo "<th class='col-md-1'></th>";
echo "</tr>";
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$cust_company = $row["cust_company"];
$cust_name = $row["cust_name"];
$cust_surname = $row["cust_surname"];
$cust_address = $row["cust_address"];
$cust_phone = $row["cust_phone"];
$cust_email = $row["cust_email"];
$cust_vat = $row["cust_vat"];
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $cust_company . "</td>";
echo "<td>" . $cust_name . "</td>";
echo "<td>" . $cust_surname . "</td>";
echo "<td>" . $cust_address . "</td>";
echo "<td>" . $cust_phone . "</td>";
echo "<td>" . $cust_email . "</td>";
echo "<td>" . $cust_vat . "</td>";
echo "<td><a class='btn btn-default' href='update-customers.php?customer=$id' role='button'>Edit</a><a class='btn btn-danger' href='delete-customers.php?customer=$id' role='button'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
}else{
echo "ERROR: Could not able to execute $query. " . mysqli_error($connection);
}}
?>

</div>
<div class="col-md-1"></div>
</div>
</div>
</div>
</div>

<!--Content-->
<?php
mysqli_close($connection);
ob_flush();
?>
<?php include($_SERVER["DOCUMENT_ROOT"] . "/admin/includes/admin-footer.php"); ?>





 

 

 


Thanks in advance!

 

 

Ben

Edited by bambinou1980
Link to comment
Share on other sites

When you go to delete-customers.php are clicking the browsers back button? If so that maybe be why, the browser will be loading your listing from the browser cache. You should either have the user click a link to go back to your listing page or use  header('Location: ...listingpagehere....);   to redirect user back to the listing

Link to comment
Share on other sites

Yes I am actually clicking a link manually from

http://sample.com/admin/crud/products/list-products.php

Showing price 0.78 in the list table

 

Now I click the "edit button" and go here:

http://sample.com/admin/crud/products/update-products.php?product=1

I change price from 0.78 to 0.85 and click update.

 

Echo message "Record updated".

 

 

Now I click on the link again:

http://sample.com/admin/crud/products/list-products.php

It shows the right update price of 0.85

 

Now I click on "Edit record"

 

It now shows the old record of 0.78, if I refresh the page, it now shows 0.85 for the same id.

 

This is so weird and it is happening on the whole script.

 

I am wondering if ob_start();  / ob_flush();

 is causing problem.

 

 

What else could cache the queries please? Would output buffering on causes this?

 

 

Thank you,

 

Ben

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.