我希望用户仅在登录后更新帐户信息......但我不知道该怎么做
I want users to update account information only after they log in... But I have no Idea How to do it
我制作了简单的 php 文件,使用这些文件我可以验证用户名和密码,然后只有用户才能登录。我希望用户只有在登录帐户后才能更新帐户。如果不验证 ID 和密码,他们就无法更新他们的姓名和所有...这是一个非常简单的程序。这是 table 结构。
这只是一个演示数据。我希望用户只有在登录后才能更新他们的帐户。这是他们可以通过登录查看他们的信息的文件。
<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>
代码运行良好,他们可以通过输入用户名和密码查看他们的数据。如果他们输入错误的用户名和密码,他们只会看到警告框。
我只是想让用户在登录后更新他们的数据。没有登录,他们不能更新他们的数据。
但我不知道该怎么做。 一旦我尝试验证用户名和密码,然后重定向到新页面,他们可以在其中使用 header 位置更新他们的帐户,但这不起作用。我在另一页上没有得到任何变量。
帮我解决一下....
因此,在登录后,不是简单地显示用户详细信息,而是显示一个允许用户更新其详细信息的表单,像这样(不完整的代码只是给你一个大纲):
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>";
}
如果你想将变量从一个页面传递到另一个页面,一旦用户登录,你应该使用Session variables。
试试这个
<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');
}
包括
- 使用
Session
- 优化查询
- 验证所有字段
也看看这个
- How can I prevent SQL-injection in PHP?
- MySQL 扩展在 PHP 5.5.0 中被弃用,并在 PHP 7.0.0 中被删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展名。
感谢大家回答我的问题。最后在你们的帮助下,我解决了所有错误,程序运行正常!
我在 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>";
}
}
?>
再次感谢大家.... :)
关于您的代码的一些注意事项:
mysql_connect 已弃用,您应该使用 mysqli_connect.
http://php.net/manual/en/book.mysqli.php
您可以使用 empty() 而不是 isset()。 empty() 将 return 如果变量是空字符串、false、array()、NULL、“0?、0 和未设置的变量。使用 !empty 你可以:
if (!empty($_POST["uname"]) && !empty($_POST["pass"])){
$uname = .........
}
不能在同一个循环中使用 echo
和 header("location:http....")
。如果你发送到另一个页面,消息将不会显示。
在 header("location:http....")
之后你必须退出();否则,代码将遵循正常流程。
你检查if ($update)
。如果单击提交按钮,$update 始终为真,因此不需要进行此检查。
希望对您有所帮助。
我制作了简单的 php 文件,使用这些文件我可以验证用户名和密码,然后只有用户才能登录。我希望用户只有在登录帐户后才能更新帐户。如果不验证 ID 和密码,他们就无法更新他们的姓名和所有...这是一个非常简单的程序。这是 table 结构。
这只是一个演示数据。我希望用户只有在登录后才能更新他们的帐户。这是他们可以通过登录查看他们的信息的文件。
<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>
代码运行良好,他们可以通过输入用户名和密码查看他们的数据。如果他们输入错误的用户名和密码,他们只会看到警告框。
我只是想让用户在登录后更新他们的数据。没有登录,他们不能更新他们的数据。
但我不知道该怎么做。 一旦我尝试验证用户名和密码,然后重定向到新页面,他们可以在其中使用 header 位置更新他们的帐户,但这不起作用。我在另一页上没有得到任何变量。
帮我解决一下....
因此,在登录后,不是简单地显示用户详细信息,而是显示一个允许用户更新其详细信息的表单,像这样(不完整的代码只是给你一个大纲):
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>";
}
如果你想将变量从一个页面传递到另一个页面,一旦用户登录,你应该使用Session variables。
试试这个
<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');
}
包括
- 使用
Session
- 优化查询
- 验证所有字段
也看看这个
- How can I prevent SQL-injection in PHP?
- MySQL 扩展在 PHP 5.5.0 中被弃用,并在 PHP 7.0.0 中被删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展名。
感谢大家回答我的问题。最后在你们的帮助下,我解决了所有错误,程序运行正常!
我在 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>";
}
}
?>
再次感谢大家.... :)
关于您的代码的一些注意事项:
mysql_connect 已弃用,您应该使用 mysqli_connect.
http://php.net/manual/en/book.mysqli.php
您可以使用 empty() 而不是 isset()。 empty() 将 return 如果变量是空字符串、false、array()、NULL、“0?、0 和未设置的变量。使用 !empty 你可以:
if (!empty($_POST["uname"]) && !empty($_POST["pass"])){
$uname = .........
}
不能在同一个循环中使用 echo
和 header("location:http....")
。如果你发送到另一个页面,消息将不会显示。
在 header("location:http....")
之后你必须退出();否则,代码将遵循正常流程。
你检查if ($update)
。如果单击提交按钮,$update 始终为真,因此不需要进行此检查。
希望对您有所帮助。