PHP development basic tutorial to implement a user registration
We make the simplest registration page. There are three parameters in the registration page:
Username
Password
Repeat Password
After the user writes the three parameters, when clicking submit, the POST record is passed to the connect.php page.
We can process the POST record and write it into the MySQL database, which completes the user registration.
The code is as follows:
<form action="connect.php" method="post"> 用户名:<input type="text" name="username"><br /> 密码:<input type="password" name="password"><br /> 重复密码:<input type="password" name="repassword"><br /> <input type="submit" value="提交"> </form>
In order to achieve faster performance, our code interface has not been beautified. We will guide you through user registration as quickly as possible.
1. Determine repeated passwords
Because there are repeated passwords, if the passwords entered by the user twice are inconsistent, it means whether the password has been entered. Any sense of the next step.
Repeated passwords are still used in many places on the web page. Because the fear is that users will make mistakes. The password was entered incorrectly.
Users may enter two more spaces on the left and right sides when entering their password. Therefore, we will use trim to remove spaces from both sides of passwords and repeated passwords.
if(trim($_POST['password']) != trim($_POST['repassword'])){ exit('两次密码不一致,请返回上一页'); }
2. Prepare the data to be written
We need to write both the user's input data and hidden data to the database.
The visible data are:
We need to remove the spaces on both sides of the username to avoid entering unnecessary information.
In the mysql chapter, we have said that the user's password should not be visible to people including company insiders. Ensure that the password is irreversible. At the initial stage, just learn MD5. We will teach you other encryption methods in the future.
Invisible data includes:
Variables | Description |
$_POST['username'] | ## Username |
$_POST['password'] | Password |
The unix timestamp returned by time
REMOTE_ADDR returns the IP address, which we can use ip2long to convert to integer storage.
$username = trim($_POST['username']); $password = md5(trim($_POST['password'])); $time = time(); $ip = ip2long($_SERVER['REMOTE_ADDR']);
3. Connect to database, judge errors, select library and character set
-
We use mysqli_connect to connect to the database server.
If there is an error, use mysqli_errno to get the error number
When there is an error mysqli_error prints out all errors and exits the program execution
Select the database and set the character set to utf8.
//连接数据库 $conn = mysqli_connect('localhost','root','liwenkaihaha'); //如果有错误,存在错误号 if(mysqli_errno($conn)){ echo mysqli_error($conn); exit; } mysqli_select_db($conn,'user'); mysqli_set_charset($conn,'utf8');
4. Combine SQL statements
We need to write the obtained information into the database. We have obtained the user name, password, creation time, and IP.
Insert the corresponding variables into the SQL statement. The combined SQL statement is as follows:
$sql = "insert into user(username,password,createtime,createip) values('" . $username . "','" . $password . "','" . $time . "','" . $ip . "')";
And our statement to create the table is as follows:
CREATE TABLE IF NOT EXISTS user ( id int(11) NOT NULL, username varchar(30) NOT NULL, password char(32) NOT NULL, createtime int(11) NOT NULL, createip int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Format of the table (field corresponding description):
## Variable | Description |
User’s registered IP |
5. Send a statement and determine the status
mysqli_query As we said above, two parameters need to be passed in:
Connected resources, the corresponding variable here is $conn.
The SQL statement sent. $sql is already prepared above.
SQL statements can be sent to the MySQL server through mysqli_query. $result is true if sent successfully. Otherwise false.
If successful, we can prompt the user that the registration is successful.
In some cases, mysqli_insert_id() may also need to be used. Print the auto-incremented primary key ID here.
Please remember this knowledge point to avoid forgetting it when needed in the future.
mysqli_insert_id application scenario: newly added row of data. We need to get the automatically growing ID value when inserting this ID value into another table. You need to use this function.
$result = mysqli_query($conn,$sql); if($result){ echo '注册成功'; }else{ echo '注册失败'; } echo '当前用户插入的ID为'.mysqli_insert_id($conn);
6. Close the database connection
Pass the resource variable to the mysqli_close function.
mysqli_close($conn);
The basic implementation code for user registration has been written. What we talked about above are code snippets.
The connect.php code we implemented is as follows:
<?php if (trim($_POST['password']) != trim($_POST['repassword'])) { exit('两次密码不一致,请返回上一页'); } $username = trim($_POST['username']); $password = md5(trim($_POST['password'])); $time = time(); $ip = $_SERVER['REMOTE_ADDR']; $conn = mysqli_connect('localhost', 'root', 'liwenkaihaha'); //如果有错误,存在错误号 if (mysqli_errno($conn)) { echo mysqli_error($conn); exit; } mysqli_select_db($conn, 'book'); mysqli_set_charset($conn, 'utf8'); $sql = "insert into user(username,password,createtime,createip) values('" . $username . "','" . $password . "','" . $time . "','" . $ip . "')"; $result = mysqli_query($conn, $sql); if ($result) { echo '成功'; } else { echo '失败'; } echo '当前用户插入的ID为' . mysqli_insert_id($conn); mysqli_close($conn); ?>
- Course Recommendations
- Courseware download
-
ElementaryNew version of php introductory tutorial
34760 people are watching -
IntermediateBand of Brothers new version PHP video tutorial
54628 people are watching -
IntermediateBand of Brothers new version Linux video tutorial
83855 people are watching -
AdvancedVideo tutorial on the new version of Band of Brothers Linux network service SSH service
11934 people are watching -
ElementaryA complete set of introductory tutorials on jQuery/Ajax/PHP basics/H5 new features
4752 people are watching -
ElementaryScala Tutorial
13417 people are watching -
ElementaryCSS Online Manual
81864 people are watching -
ElementarySVG Tutorial
12876 people are watching -
ElementaryAngularJS Chinese Reference Manual
24320 people are watching -
ElementaryGo language tutorial manual
27114 people are watching -
ElementaryRedis command operation Chinese manual
57613 people are watching -
ElementaryPython 3 Tutorial
87298 people are watching
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
id | username | ##password | createtime | createip |
Create IP
|