Home  >  Article  >  Database  >  MySQL implements the user login and registration function of the ordering system

MySQL implements the user login and registration function of the ordering system

王林
王林Original
2023-11-01 16:22:121003browse

MySQL 实现点餐系统的用户登录注册功能

MySQL implements the user login and registration function of the ordering system

When developing the ordering system, the user login and registration function is an essential part. This article will introduce how to use the MySQL database to implement the user login and registration function of the ordering system, and give specific code examples.

  1. Create user table

First, we need to create a user table in the MySQL database to store the user's login registration information. The user table needs to contain the following fields: user ID (primary key), user name, password.

You can use the following SQL statement to create a user table:

CREATE TABLE user (
  id INT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(30) NOT NULL,
  password VARCHAR(255) NOT NULL
);
  1. User registration function implementation

The user registration function requires the user name and password entered by the user to be inserted into the user table. You can follow the following steps to implement the user registration function:

  • On the registration page, collect the user name and password entered by the user.
  • In the backend code, use a MySQL statement to insert the username and password into the users table.

The following is an example of using PHP code to implement the user registration function:

<?php
// 获取用户输入的用户名和密码
$username = $_POST['username'];
$password = $_POST['password'];

// 连接MySQL数据库
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}

// 将用户名和密码插入到用户表中
$sql = "INSERT INTO user (username, password) VALUES ('$username', '$password')";

if ($conn->query($sql) === TRUE) {
  echo "注册成功";
} else {
  echo "注册失败: " . $conn->error;
}

$conn->close();
?>
  1. User login function implementation

The user login function needs to be checked Whether the username and password entered by the user match the records in the users table. You can follow the following steps to implement the user login function:

  • On the login page, obtain the user name and password entered by the user.
  • In the back-end code, use the MySQL statement to query the user table and check whether the user name and password match.

The following is an example of using PHP code to implement user login function:

<?php
// 获取用户输入的用户名和密码
$username = $_POST['username'];
$password = $_POST['password'];

// 连接MySQL数据库
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}

// 查询用户表,检查用户名和密码是否匹配
$sql = "SELECT * FROM user WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);

// 检查查询结果是否为空
if ($result->num_rows > 0) {
  echo "登录成功";
} else {
  echo "登录失败";
}

$conn->close();
?>

In the above example, we use basic PHP code and MySQL statements to implement user login registration Function. In actual development, we also need to consider the security of user names and passwords, such as encrypted password storage, input verification, etc.

The above is the detailed content of MySQL implements the user login and registration function of the ordering system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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