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

时间:2015-01-13 14:37:32

标签: php mysql ajax forms mysqli

Index.php页面 我是初学者。 这是索引页面,其中我打印了登录用户的名称,并且我想在用户编辑他的详细信息时更新用户名我尝试更新它,它更新用户名但是当我编辑详细信息然后我首先注销然后再次登录然后名称将更新。我想在用户编辑他的详细信息时立即更新名称,并像facebook一样重定向到索引页面。 我几乎花了一个星期来解决这个问题。如果需要使用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>

1 个答案:

答案 0 :(得分:0)

更新数据库时,您没有更改$ _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中的某处回复消息!