我正在寫一個應用程序到CRUD用戶的詳細信息。我通過對用戶表的查詢啓動頁面 - 並使用數據填充頁面中的各種表單元素(並檢查重複項)。所有表單提交提交到同一頁面。MySQL更新記錄,但頁面顯示不顯示新數據
添加,刪除和修改記錄按預期工作 - 但踢球是顯示在頁面上一次數據的變化提交不相應地更新 - 我要訪問該網頁才能看到相關變更。
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>';
這是非常進展中的工作,所以它需要大量的重構......
從用戶的角度來看:
1:輸入新數據
2:提交表格
3:從MySQL控制檯檢查數據庫 - 插入新數據
4:沒有新數據顯示
5:訪問頁面再次 - 新的數據顯示(清爽將觸發試圖重新提交重複值的誤差)
任何人有任何想法是怎麼回事?目前,我正在將重定向回頁面頭部的想法...
顯示整個代碼取所以人們將能夠理解正在發生的事情。從你的話來說,這很難理解。 – nightcoder 2010-01-24 20:45:41
就是這樣 - 所有提交過程都發生在表單本身內部。 – sunwukung 2010-01-25 09:25:01
Doh!我現在看到它了......我曾假設因爲頁面被刷新了,它會從主機發回數據 - 但顯然這部分頁面不會在主機上執行,直到更新之後... 我現在感覺像一個扳手! – sunwukung 2010-01-25 12:50:22