PHP develops user signature and personal data settings for real-time chat system
Introduction:
In real-time chat system, user signature and personal data settings are very important One of the important functions. Users can display their own information by setting up a personal profile, and can customize a unique personality signature to express their personality and attitude. This article will introduce how to implement user signature and profile setting functions in a real-time chat system developed in PHP.
CREATE TABLE `users` ( `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, `username` varchar(50) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(50) NOT NULL, `signature` varchar(100) DEFAULT NULL, `profile_pic` varchar(255) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() );
In this table, we define some basic fields, such as user name, password, email, etc. At the same time, we added a field signature for storing user signatures and a field profile_pic for storing user avatars.
<form action="profile_update.php" method="POST"> <label>用户名:</label> <input type="text" name="username" value="<?php echo $username; ?>" required> <br> <label>邮箱:</label> <input type="email" name="email" value="<?php echo $email; ?>" required> <br> <label>个性签名:</label> <textarea name="signature"><?php echo $signature; ?></textarea> <br> <label>头像:</label> <input type="file" name="profile_pic"> <br> <input type="submit" value="保存"> </form>
In this page, we use a form to display the user's personal information and provide a submit button to save the information entered by the user. Note that we used to fill in the default value in the form, where $username is the user information obtained from the database.
<?php // 连接数据库 $db = new mysqli('localhost', 'username', 'password', 'database_name'); // 检查连接是否成功 if ($db->connect_errno) { die('连接数据库失败:' . $db->connect_error); } // 获取用户提交的信息 $username = $_POST['username']; $email = $_POST['email']; $signature = $_POST['signature']; $profile_pic = $_FILES['profile_pic']['name']; $tmp_name = $_FILES['profile_pic']['tmp_name']; // 保存用户信息到数据库 $sql = "UPDATE `users` SET `username`='$username', `email`='$email', `signature`='$signature', `profile_pic`='$profile_pic' WHERE `id` = $user_id"; if ($db->query($sql) === TRUE) { echo "用户信息已保存"; } else { echo "保存失败:" . $db->error; } // 保存用户头像到服务器 move_uploaded_file($tmp_name, 'profile_pics/' . $profile_pic); // 关闭数据库连接 $db->close();
In this file, we first connect to the database and obtain the personal information submitted by the user. We then use the UPDATE statement to update the user information into the database. Finally, we use the move_uploaded_file function to save the avatar uploaded by the user to the specified directory on the server.
Summary:
Through the above steps, we have implemented the user signature and profile setting functions. Users can fill in and modify personal information and upload avatars on the profile settings page. All user information will be saved in the database, and the avatar uploaded by the user will be saved in the specified directory on the server. this
The above is the detailed content of PHP develops user signature and profile settings for real-time chat system. For more information, please follow other related articles on the PHP Chinese website!