Home > Backend Development > PHP Tutorial > PHP develops user signature and profile settings for real-time chat system

PHP develops user signature and profile settings for real-time chat system

WBOY
Release: 2023-08-13 11:38:02
Original
1606 people have browsed it

PHP develops user signature and profile settings for real-time chat system

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.

  1. Database Design
    First, we need to create a table in the database to store the user's profile information. The following is a simple user table design example:
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()
);
Copy after login

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.

  1. User profile setting page
    Next, we need to create a page for user profile setting so that users can fill in personal information on the page and save it.
<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>
Copy after login

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.

  1. User profile saving
    Next, we need to create a file profile_update.php for processing form submission data. In this file, we will receive the personal information submitted by the user and save it into 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();
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template