如何在 Edit.php 页面上编辑后更新 Index.php 页面上的数据库值

How To Update Database Value On Index.php Page After Editting On Edit.php Page

Index.php 页 我是初学者。 这是索引页面,我在其中打印已登录用户的名称,并且我想在用户编辑他的详细信息时更新用户名我尝试更新它,它正在更新用户名但是当我编辑详细信息然后我先注销然后再次登录然后名称将 update.I 当用户编辑他的详细信息并重定向到索引页面时立即更新名称,就像 facebook 一样。 我几乎花了一个星期来解决这个 problem.Please 如果有任何需要 ajax 给我代码。 谢谢

<?php
session_start();
require_once("inc/connection.php");
if (empty($_SESSION['usersession'])) {
    header("Location: login.php");
}
if (isset($_SESSION['msg'])) {
    echo $_SESSION['msg'];
    unset($_SESSION['msg']);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>WELCOME TO USER AREA</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <div id="main">
            <nav id="nav">
                <div class="logout">
                    WELCOME <?php echo $_SESSION['usersession']['name']; ?> // echo name of user
                    <a href="logout.php">Log out</a>        
                </div>
                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="register.php">Register</a></li>
                    <li><a href="edit.php">Edit</a></li>
                    <li><a href="delete.php">Delete(not recommended)</a></li>
                </ul>
            </nav>
            <div class="para">
                some text
            </div>
        </div>
    </body>
</html>

Edit.php 这是 edit.php 代码

<?php
session_start();
if (empty($_SESSION['usersession'])) {
    header("Location: login.php");
}
if (isset($_SESSION['msg'])) {
    echo $_SESSION['msg'];
    unset($_SESSION['msg']);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>REGISTER HERE</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <div id="reg2">
            <?php
            if (isset($_POST['btn'])) {
                $name = $_POST['name'];
                $email = $_POST['email'];
                $password = $_POST['password'];
                $id = $_SESSION['usersession']['id'];
                if (!empty($name && $email && $password)) {
                    if (!empty($password)) {
                        $password = sha1($password);
                        require_once("inc/connection.php");
                        $query = mysqli_query($conn, "UPDATE register SET name='$name',email='$email',password='$password' WHERE id='$id'");
                        if ($query) {
                            header("Location: index.php");
                        } else {
                            echo "not updated right now please try again later";
                        }
                        $_SESSION['msg'] = "Your detail has been updated successfully";
                    }
                } else {
                    echo "please put your updated password";
                }
            }
            ?>
        </div>
        <div id="form">
            <form method="post" action="edit.php">
                <label>NAME</label><p>
                <input class="int" type="text" class="nm" name="name" placeholder="Please Enter Your Name Here" value="<?php echo $_SESSION['usersession']['name']; ?>" /><p>
                    <label>EMAIL</label><p>
                <input class="int" type="email" class="em" name="email" placeholder="Please Enter Your Email Here" value="<?php echo $_SESSION['usersession']['email']; ?>"  /><p>
                    <label>PASSWORD</label><p>
                    <input class="int" type="password" name="password" placeholder="Please Enter Your new Password Here" />
                <p></p>
                <input type="hidden" name="id[]" value="<?php echo $_SESSION['usersession']['id']; ?>"/>
                <input type="submit" name="btn" id="btu" value="Update">
            </form>
        </div>
    </body>
</html>

更新数据库时,您没有更改 $_SESSION 中的值。 您必须手动执行此操作。

// Add that under $_SESSION['msg'] = '....'
$_SESSION['usersession']['username'] = $name; // Same with email and password

请注意,您的代码容易受到 mysql 注入攻击。你应该mysql_real_escape_string to avoid that.

此外,您甚至在输出 <!DOCUMENT html> 之前就显示了您的消息。请不要那样做。而是在 html!

中的某处回显消息