我希望用户只有在登录后才能更新帐户信息...但我不知道怎么做

时间:2015-09-22 08:56:33

标签: php mysql xampp

我已经制作了简单的php文件,我可以使用它来验证用户名和PASSWORD,然后只有用户才能登录。我希望用户只有在登录帐户时才能更新帐户。在没有验证ID和密码的情况下,他们无法更新他们的名字和姓氏以及所有...这是非常简单的程序。这是表格结构。

enter image description here

这只是一个演示数据。我希望用户只有在登录后才能更新他们的帐户。这是他们通过登录查看信息的文件。

    <html>
    <head>
        <title>
            Login
        </title>
    </head>
    <body>
<?php
if(isset($_POST["uname"]) && isset($_POST["pass"]))
{
    $uname=$_POST["uname"];
    $pass=$_POST["pass"];
    mysql_connect("localhost","adarsh","Yeah!");
    mysql_select_db("aadarsh");
    $select = mysql_query("select * from users where username='$uname' AND pass='$pass'");
    $data = mysql_fetch_array($select);
    if($uname==$data['username'] && $pass==$data['pass'])
    {
        echo "<center>";

        echo "Name: ".$data['username']."<br>";
        echo "Last namme: ".$data['lastname']."<br>";
        echo "<img src=".$data['image']."><br>";
        echo "</center>";
    }
    else
    {
        echo "<script>alert('Nope!!!');</script>";
    }
}
?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <input type="text" name="uname">
        <input type="pass" name="pass">
        <input type="submit" name="submit" value="Login!">
    </form>
</html>

代码工作正常,他们可以通过输入用户名和密码来查看他们的数据。如果他们输入错误的用户名和密码,他们只会看到警告框。

我只是希望用户在登录后更新他们的数据。没有登录,他们就无法更新他们的数据。

但我不知道该怎么做。 一旦我尝试验证用户名和密码,然后重定向到新页面,他们可以使用标题位置更新其帐户,但这不起作用。我在另一页上没有得到任何变数。

帮我解决这个问题....

5 个答案:

答案 0 :(得分:2)

试试这个

<html>
    <head>
        <title>
            Login
        </title>
    </head>
    <body>
        <?php
            session_start();
            if(isset($_POST["submit"]))
            {
                $uname=$_POST["uname"];
                $pass=$_POST["pass"];
                if(empty($uname) && empty($pass))
                {
                    echo "<script>alert('Empty');</script>";
                }
                else
                {
                    mysql_connect("localhost","adarsh","Yeah!","aadarsh");

                    $select = mysql_query("select * from users where username='$uname' AND pass='$pass'");
                    $data = mysql_fetch_array($select);

                    $count = count($data);
                    if(empty($count) || $count > 1)
                    {
                        echo "<script>alert('Invalid Login');</script>";
                    }
                    else
                    {
                        $image = $data['image'];
                        $lname = $data['lastname'];
                        $username = $data['username'];

                        $_SESSION["lastname"] = $lname;
                        $_SESSION["username"] = $username;

                        echo "Name: ".'$username'."<br>";
                        echo "Last namme:".'$lname'."<br>";
                        echo "<img src='$image'><br>";

                        if(isset($_SESSION))
                        {
                            redirect('new_page.php');
                        }
                        else
                        {
                            echo "<script>alert('Something Went Wrong');</script>";
                        }

                    }

                }

            }
        ?>
        <form method="post" action="#">
            <input type="text" name="uname">
            <input type="pass" name="pass">
            <input type="submit" name="submit" value="Login!">
        </form>
    </body>
</html>

并在new_page.php

<?php
    session_start();

    if(isset($_SESSION["username"]))
    {
        //show update form
    }
    else
    {
        //redirect to login page
        redirect('login.php');
    }

<强>含

  1. 使用Session
  2. 优化查询
  3. 验证所有字段
  4. 并看一下

    1. How can I prevent SQL-injection in PHP?
    2. 在PHP 5.5.0中不推荐使用MySQL扩展,它已在PHP 7.0.0中删除。相反,应该使用MySQLi或PDO_MySQL扩展。

答案 1 :(得分:0)

因此,登录后,不是简单地显示用户详细信息,而是显示一个表单,允许用户更新他们的详细信息,如下所示(不完整的代码只是为了给你一个大纲):

if($uname==$data['username'] && $pass==$data['pass'])
{
    echo '<form method="" action ="">';

    echo '<input value="'.$data['username'].'" />';
    echo '<input value="'.$data['lastname'].'" />';
    echo '<input type="submit" />';
    echo "</form>";
}

答案 2 :(得分:0)

如果要将变量从一个页面传递到另一个页面,一旦用户登录,您应该使用Session variables

答案 3 :(得分:0)

感谢所有人回答我的问题。最后在你们的帮助下,我解决了所有错误,程序工作正常! 我是在2个文件的帮助下做到的......这是他们的,

updatedata.php (此文件仅包含html内容... .html 也可以使用)

    <html>
    <head>
        <title>
            Login
        </title>
    </head>
    <body>
    <form method="post" action="updateaccount.php">
        Username : <input type="text" name="uname"><br>
        Password :<input type="password" name="pass"><br>
        New Information:<br><br>
        New Name : <input type="text" name="newname"></input>
        <input type="submit" name="submit" value="Update!">
    </form>
</html>

updateaccount.php (呵呵,不要混淆文件名......)

    <?php 
$con=mysql_connect("localhost","adarsh","Password"); 
mysql_select_db("aadarsh",$con);
if(isset($_POST["uname"]) && isset($_POST["pass"]))
{   
    $uname=$_POST["uname"];
    $pass=$_POST["pass"];
}
    $sql="select * from users where username='$uname' AND pass='$pass'";
    $select = mysql_query($sql);
    $data = mysql_fetch_array($select);
    $username=$_POST["newname"];
if(isset($_POST['submit']))
{
    if($uname==$data['username'] && $pass==$data['pass'])
    {
        $user_id= $data['id'];
        if(isset($_POST['newname']))
        {
            $update = mysql_query("UPDATE users SET username = '$username' WHERE id = $user_id");       
            if($update)
            {
                echo "<script>alert('updated!');</script>";
                header("location:http://www.example.com");
            }
            else
            {
                echo mysql_error();
            }
        }
    }
    else
    {
        echo "<script>alert('Nope!!!');</script>";
    }
}
?>

再次感谢大家......:)

答案 4 :(得分:0)

关于代码的一些注意事项:

不推荐使用mysql_connect,你应该使用mysqli_connect。

http://php.net/manual/en/book.mysqli.php

您可以使用empty()而不是isset()。如果变量是空字符串,false,array(),NULL,“0?,0和未设置的变量”,则empty()将返回true。有了!空,你可以:

    if (!empty($_POST["uname"]) && !empty($_POST["pass"])){
          $uname = .........
    }

无法在同一循环中使用echoheader("location:http....")。如果您发送到另一个页面,则不会显示该消息。

header("location:http....")之后你必须退出();否则,代码将遵循正常流程。

您检查if ($update)。如果单击“提交”按钮,$ update始终为true,因此不需要进行此检查。

希望有所帮助。