Home >Backend Development >PHP Tutorial >Second-hand recycling website developed by PHP to implement user account setting function
The second-hand recycling website developed by PHP realizes the user account setting function
With the progress of society and the improvement of environmental awareness, second-hand recycling has become a trend. In order to facilitate people's transaction of second-hand recycling items, it is very necessary to develop a second-hand recycling website. This article will introduce how to use PHP to develop a second-hand recycling website and implement the user account setting function.
First, we need to create a database to store user information and settings. Taking MySQL as an example, you can use the following SQL statement to create a table named "users":
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In the registration page of the website, users Username, password and email address are required. When the user clicks the submit button, we need to save the information entered by the user into the database.
<form action="register.php" method="post"> <label for="username">用户名:</label> <input type="text" name="username" id="username" required> <br> <label for="password">密码:</label> <input type="password" name="password" id="password" required> <br> <label for="email">电子邮件:</label> <input type="email" name="email" id="email" required> <br> <input type="submit" value="注册"> </form>
In the registration page, we use a page named "register.php" to process the form information submitted by the user. In this page, we need to connect to the database and then insert the information entered by the user into the "users" table.
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 处理用户提交的表单 $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; // 将用户信息插入数据库 $sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')"; mysqli_query($conn, $sql); // 关闭数据库连接 mysqli_close($conn); // 跳转到登录页面 header("Location: login.php"); ?>
In the login page, after the user enters the username and password and clicks the submit button, we need to verify whether the user's login information is correct. If correct, jump to the home page; otherwise, display an error message.
<form action="login.php" method="post"> <label for="username">用户名:</label> <input type="text" name="username" id="username" required> <br> <label for="password">密码:</label> <input type="password" name="password" id="password" required> <br> <input type="submit" value="登录"> </form>
In the login page, we use a page named "login.php" to process the login information submitted by the user. In this page, we need to connect to the database and verify the user's login information by querying the user information in the database.
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 处理用户提交的表单 $username = $_POST['username']; $password = $_POST['password']; // 查询数据库中是否存在该用户 $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $sql); // 如果查询结果不为空,表示登录信息正确,跳转到主页 if (mysqli_num_rows($result) > 0) { header("Location: homepage.php"); } else { // 否则,显示错误信息 echo "用户名或密码错误"; } // 关闭数据库连接 mysqli_close($conn); ?>
Through the above steps, we have completed the user registration and login functions. Next, we can implement the user's account setting function.
In the account settings page, users can change their username, password and email address. Users need to enter their current password for verification before they can modify their account information.
<form action="account.php" method="post"> <label for="username">用户名:</label> <input type="text" name="username" id="username" required> <br> <label for="current_password">当前密码:</label> <input type="password" name="current_password" id="current_password" required> <br> <label for="new_password">新密码:</label> <input type="password" name="new_password" id="new_password"> <br> <label for="email">电子邮件:</label> <input type="email" name="email" id="email" required> <br> <input type="submit" value="保存"> </form>
In the account setting page, we use a page named "account.php" to process the modification information submitted by the user. In this page, we need to connect to the database and verify whether the current password is correct by querying the user information in the database. If the verification passes, we will update the user information in the database.
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 处理用户提交的表单 $username = $_POST['username']; $current_password = $_POST['current_password']; $new_password = $_POST['new_password']; $email = $_POST['email']; // 查询数据库中是否存在该用户,并验证当前密码是否正确 $sql = "SELECT * FROM users WHERE username='$username' AND password='$current_password'"; $result = mysqli_query($conn, $sql); // 如果查询结果不为空,表示用户身份验证通过,更新用户信息 if (mysqli_num_rows($result) > 0) { $sql = "UPDATE users SET password='$new_password', email='$email' WHERE username='$username'"; mysqli_query($conn, $sql); echo "账号信息已保存"; } else { // 否则,显示错误信息 echo "当前密码不正确"; } // 关闭数据库连接 mysqli_close($conn); ?>
Through the above steps, we have implemented the user's account setting function. Users can modify their username, password and email address through the account settings page.
When developing a second-hand recycling website, the user account setting function is a very important part. Users can modify account information according to their own needs, providing convenience for completing second-hand item transactions quickly and conveniently. Through the previous code example, we can see how to use PHP to achieve this functionality. Of course, in actual development, we also need to consider issues such as security and user experience, which require further improvement and optimization. I hope this article will be helpful to developers in implementing the user account setting function.
The above is the detailed content of Second-hand recycling website developed by PHP to implement user account setting function. For more information, please follow other related articles on the PHP Chinese website!