在向我的数据库提交信息后,我想刷新页面以显示这些更改,就像表单处理完毕一样.提交后页面“重新加载”但不反映更改,所以我假设我需要在按下提交时添加刷新命令,但它似乎太快了?
所以我添加了一个刷新时间,但即使把它调到50,我也得到了相同的结果.
如果我按两次按钮,则会刷新正确的信息.有一个更好的方法吗?
<?PHP ini_set('display_errors',1); error_reporting(E_ALL); include_once '../includes/conn.PHP'; if(!$user->is_loggedin()){ $user->redirect('../users/login.PHP'); } $id = $_SESSION['session']; $stmt = $conn->prepare("SELECT * FROM users WHERE id=:id"); $stmt->execute(array(":id"=>$id)); $userRow=$stmt->fetch(PDO::FETCH_ASSOC); $location = isset($_POST['location']) ? $_POST['location'] : ''; $about = isset($_POST['about']) ? $_POST['about'] : ''; $title = isset($_POST['title']) ? $_POST['title'] : ''; if($title!=''){ $sql = "UPDATE users SET title=:title WHERE id=:id"; $stmt = $conn->prepare($sql); if($stmt == false){ $error = "User Title update Failed. Please try again."; } $result = $stmt->execute(array(":title"=>$title,":id"=>$id)); if($result == false) { $error = "User Title update Failed. Please try again."; } $count = $stmt->rowCount(); } if($location!=''){ $sql = "UPDATE users SET location=:location WHERE id=:id"; $stmt = $conn->prepare($sql); if($stmt == false){ $error = "User Location update Failed. Please try again."; } $result = $stmt->execute(array(":location"=>$location,":id"=>$id)); if($result == false) { $error = "User location update Failed. Please try again."; } $count = $stmt->rowCount(); } if($about!=''){ $sql = "UPDATE users SET about=:about WHERE id=:id"; $stmt = $conn->prepare($sql); if($stmt == false){ $error = "about Me update Failed. Please try again."; } $result = $stmt->execute(array(":about"=>$about,":id"=>$id)); if($result == false) { $error = "about Me location update Failed. Please try again."; } $count = $stmt->rowCount(); } ?> <!DOCTYPE html> <html lang="en"> <head> <title>EpicOwl UK | CMS Users Edit Profile</title> <Meta charset="utf-8"> <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" /> <link rel="stylesheet" type="text/css" href="../css/main.css"> </head> <body> <div id="header"> <a href="index.PHP"><img id="logo" src="../images/logo.png" /></a> <div id="navigation"> <ul> <a href="../index.PHP"><li>Home</li></a> <a href="./profile.PHP"><li>My Profile</li></a> <a href="../admin/index.PHP"><li>Admin Panel</li></a> </ul> </div> </div> <div id="content"> <form method="post"><br /> <h2>Edit Profile</h2> <label><strong>User Title:</strong></label><br /> <input type="text" name="title" maxlength="50" placeholder="<?PHP echo ($userRow['title']); ?>" /><br /><br /> <label><strong>My Location:</strong></label><br /> <input type="text" name="location" maxlength="50" placeholder="<?PHP echo ($userRow['location']); ?>" /><br /><br /> <label><strong>About Me:</strong><label><br /> <textarea name="about" rows="13" cols="60" maxlength="255" placeholder="<?PHP echo ($userRow['about']); ?>"></textarea><br /><br /> <button type="submit" name="update">Update</button><br /><br /><br /> <?PHP if(isset($_POST['submit'])){ header('refresh:20; Location: '.$_SERVER['REQUEST_URI']); } ?> </form> </div> <div id="footer"> <p class="copyright">© EpicOwl UK. All Rights Reserved.</p> </div> </body> </html>
你做错了,你必须在显示HTML之前处理表单提交. PHP正在逐行执行,因此在您的情况下,您首先显示数据,然后检查表单是否已提交.只需将此代码移到PHP代码的其余部分所在的位置(您甚至可以删除refresh stuff命令):
原文链接:https://www.f2er.com/php/139155.htmlif(isset($_POST['submit'])){ header('Location: '.$_SERVER['REQUEST_URI']); }
编辑:
当你混合使用HTML和PHP时,人们发明MVC是因为像你这样的情况,并且想知道为什么事情不起作用.将PHP代码保存在文件的顶部,尽量不要在HTML中的任何地方编写PHP代码,这样可以省去很多麻烦.此外,在调用header之后使用exit来进一步停止代码执行.这是您的代码的更新版本,简化和更“算法”(我希望您看到并理解代码流的方式):
<?PHP ini_set('display_errors',1); error_reporting(E_ALL); include_once '../includes/conn.PHP'; if(!$user->is_loggedin()){ $user->redirect('../users/login.PHP'); } $id = $_SESSION['session']; $stmt = $conn->prepare("SELECT * FROM users WHERE id=:id"); $stmt->execute(array(":id"=>$id)); $userRow=$stmt->fetch(PDO::FETCH_ASSOC); if (isset($_POST['submit'])) { $location = isset($_POST['location']) ? $_POST['location'] : null; $about = isset($_POST['about']) ? $_POST['about'] : null; $title = isset($_POST['title']) ? $_POST['title'] : null; $sql_part = array(); $prepare = array(); if ($location) { $sql_part[] = 'location = :location'; $prepare[':location'] = $location; } if ($about) { $sql_part[] = 'about = :about'; $prepare[':about'] = $about; } if ($title) { $sql_part[] = 'title = :title'; $prepare[':title'] = $title; } $prepare[':id'] = $id; if (count($sql_part)) { $sql = 'UPDATE users SET '; $sql .= implode(',',$sql_part); $sql .= ' WHERE id = :id'; $stmt = $dbh->prepare($sql); if ($stmt) { // Find another way too pass these through the refresh // $result = $stmt->execute($prepare); // $count = $stmt->rowCount(); header('Location: '. $_SERVER['REQUEST_URI']); exit; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>EpicOwl UK | CMS Users Edit Profile</title> <Meta charset="utf-8"> <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" /> <link rel="stylesheet" type="text/css" href="../css/main.css"> </head> <body> <div id="header"> <a href="index.PHP"><img id="logo" src="../images/logo.png" /></a> <div id="navigation"> <ul> <a href="../index.PHP"><li>Home</li></a> <a href="./profile.PHP"><li>My Profile</li></a> <a href="../admin/index.PHP"><li>Admin Panel</li></a> </ul> </div> </div> <div id="content"> <form method="post"><br /> <h2>Edit Profile</h2> <label><strong>User Title:</strong></label><br /> <input type="text" name="title" maxlength="50" placeholder="<?PHP echo ($userRow['title']); ?>" /><br /><br /> <label><strong>My Location:</strong></label><br /> <input type="text" name="location" maxlength="50" placeholder="<?PHP echo ($userRow['location']); ?>" /><br /><br /> <label><strong>About Me:</strong><label><br /> <textarea name="about" rows="13" cols="60" maxlength="255" placeholder="<?PHP echo ($userRow['about']); ?>"></textarea><br /><br /> <button type="submit" name="update">Update</button><br /><br /><br /> </form> </div> <div id="footer"> <p class="copyright">© EpicOwl UK. All Rights Reserved.</p> </div> </body> </html>