MySQL updating records, but page display not showing new data
I am writing an application to CRUD user details. I kick off the page with a query to the user t开发者_如何学编程able - and use the data to populate various form elements in the page (and to check for duplicates). All form submissions submit to the same page.
Adding, deleting and modifying records work as expected - but the kicker is that the data displayed on the page does not update accordingly once a change is submitted - I have to revisit the page in order to see the relevant changes.
try{
$user_sql = "SELECT user_ID, user_firstname,user_surname,user_email,user_type FROM users";
$user_stmt = $db->prepare($user_sql);
$user_stmt->execute();
$user_data = $user_stmt->fetchAll(PDO::FETCH_NAMED);
$i++;
}catch(PDOException $e){
echo "Error: cannot retrieve user data from the data base";
}
/*
* DELETE USER
*/
if(isset($_POST['deleteUser'])){
$_SESSION['deleteUser']=true;
}
if(isset($_POST['submitDeleteUserConfirm'])){
//process query
if($_POST['deleteUserConfirm']=='yes'){
$deleteRange=implode(',',$_POST['deleteUser']);
$deleteSql = 'DELETE FROM users WHERE ID_users IN (' . $deleteRange . ')';
try{$q = $db->prepare($deleteSql);
$q->execute();
}
catch(PDOException $e){
echo '<p>System Error: '. $e->getMessage() .'</p>';
}
}
//once confirmation has been processed:
//remove session trigger to hide confirmation form
if(isset($_SESSION['deleteUser'])){
unset($_SESSION['deleteUser']);
}
}
if(isset($_SESSION['deleteUser'])){
?>
<fieldset class="radiobox">
<legend>Confirm</legend>
<div>
<label for="deleteUserYes">Yes</label>
<input type="radio" class="radio" name="deleteUserConfirm" id="deleteUserYes" value="yes">
</div>
<div>
<label for="deleteUserNo">No</label>
<input type="radio" class="radio" name="deleteUserConfirm" id="deleteUserNo" value="no" checked>
</div>
<input type="submit" name="submitDeleteUserConfirm" value="Confirm">
</fieldset>
<?php
}
//small function to output all the elements of an array as checkboxes
$delStudentRollOpts = array(
'key'=>'users',
'sticky'=>true,
'data'=>array(
'values'=>$user_data,# USES DATA FROM START OF SCRIPT - NOT UPDATING ON PAGE REFRESH/FORM SUBMISSION
'name'=>array('user_firstname','user_surname')
),
'element_name'=>'delStudRoll_'.$n,
'types'=>array('checkbox'=>array('title'=>' ',
'name'=>'deleteUser',
'index'=>'ID_users'
))
);
echo $GA_form->generateRoll($delStudentRollOpts);
echo '<input type="submit" class="submit" name="submitDeleteUser_'.$n.'" value="Delete">';
echo '</fieldset>';
echo '</form>';
This is very much a work in progress, so it needs a lot of refactoring...
From the user's perspective:
1: enter new data
2: submit form
3: check db from MySQL console - new data inserted
4: no new data shown
5: visit page again - new data appears (refreshing will trigger an error trying to re-submit duplicate values)
Anyone have any ideas what's happening? At the moment I'm toying with the idea of redirecting back to the page with a header...
if(form_submitted) {
update_database()
}
fetch_data();
<form></form>
You need to look in to your sequence of steps. Update probably happens after the users are fetched.
Either you have some browser caching problems or you fetch before* the insert/update.
Try looking in database directly to see if the users are being inserted to dismiss browser caching. If it isn't that, it must be the fetching order problem :)
If nothing of these works either, try putting sleep(1) between the insert and fetch just in case :)
精彩评论