php - sql更新后刷新页面

标签 php

将信息提交到我的数据库后,我想刷新页面以显示这些更改,就像处理表单时一样。提交后页面“重新加载”,但没有反射(reflect)更改,所以我假设我需要在按下提交时添加刷新命令,但这似乎太快了?

所以我添加了刷新时间,但即使将其提高到 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">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>

最佳答案

您做错了,您必须在显示 HTML 之前处理表单提交。 PHP 是逐行执行的,因此在您的情况下,您首先显示数据,然后检查表单是否已提交。只需将此代码移至其余 PHP 代码所在的位置(您甚至可以删除刷新内容命令):

if(isset($_POST['submit'])){
    header('Location: '.$_SERVER['REQUEST_URI']);
    die;
}
<小时/>

编辑:

人们之所以发明 MVC,是因为像您这样的情况,当您混合 HTML 和 PHP 并想知道为什么事情不起作用时。将 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">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>

关于php - sql更新后刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728660/

相关文章:

php - 在登录 PHP MySql 时检索加盐 MD5 密码

php - 没有数据库的 Codeigniter 网站在实时服务器上生成数据库错误

javascript - 如果通过 javascript 或 jquery,背景更改宽度

php - Codeigniter 3 显示所有在线用户

php - 使用 PHP 变量加载 View 的一部分

php - Woocommerce - 尝试提取订单的运费

javascript - 如果 else 在我的 php 代码中无法正常工作

php - MySQL Select 语句不输出任何内容

php - 如何将模型函数附加到模型结果

php - 通过php将数据插入mysql多对多关系