我正在编写一个针对CRUD用户详细信息的应用程序.我通过查询用户表开始页面-并使用数据填充页面中的各种表单元素(并检查重复项).所有表单提交都提交到同一页面.
添加,删除和修改记录按预期方式工作-但最重要的是,提交更改后,页面上显示的数据不会相应更新-我必须重新访问页面才能查看相关更改.
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 '
System Error: '. $e->getMessage() .'
';}
}
//once confirmation has been processed:
//remove session trigger to hide confirmation form
if(isset($_SESSION['deleteUser'])){
unset($_SESSION['deleteUser']);
}
}
if(isset($_SESSION['deleteUser'])){
?>
Confirm
Yes
No
}
//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 '';
echo '';
echo '';
这是一项正在进行的工作,因此需要大量重构…
从用户的角度来看:
1:输入新数据
2:提交表格
3:从MySQL控制台检查数据库-插入了新数据
4:未显示新数据
5:再次访问页面-出现新数据(刷新将触发尝试重新提交重复值的错误)
任何人都知道发生了什么事吗?目前,我正在用重定向到带有标题的页面的想法.
解决方法:
if(form_submitted) {
update_database()
}
fetch_data();
标签:mysql,php
来源: https://codeday.me/bug/20191106/2001638.html