Implement user registration function
Let’s make the simplest registration page. There are three parameters in the registration page:
1. Username
2. Password
3. Repeat password
After the user writes the three parameters, When you click 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 duplicate passwords
Because there are duplicate passwords, if the passwords entered by the user twice are inconsistent, it means that the user has not proceeded to the next step. any sense.
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.
Visible data are:
1. We need to remove the spaces on both sides of the username to avoid unnecessary input of these information.
2. In the mysql chapter, we mentioned that the user’s password should not be visible to anyone 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 dataThere are:
1. The unix timestamp returned by time
2. Returned by REMOTE_ADDR is the IP address, we can use ip2long to convert it to integer storage.
$username = trim($_POST['username']); $password = md5(trim($_POST['password'])); $time = time(); $ip = ip2long($_SERVER['REMOTE_ADDR']);
3. Connecting to the database, judging errors, selecting libraries and character sets
1. We use mysqli_connect to connect to the database server.
2. If there is an error, use mysqli_errno to get the error number
3. If there is an error mysqli_error prints out all the errors and exits the program execution
4. Select the database And set the character set to utf8.
//Connect to the database
$conn = mysqli_connect('localhost','root','123456789');
//If there is an error, there is an error number
if(mysqli_errno($conn)){ echo mysqli_error($conn); exit; } mysqli_select_db($conn,'book'); mysqli_set_charset($conn,'utf8');
4. Combined SQL Statement
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):
5. Send a statement and determine the status
mysqli_query As we said above, two parameters need to be passed in:
1. The connected resource, the corresponding variable here is $conn.
2. 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);
5. 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', '123456789'); //如果有错误,存在错误号 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); ?>