PHP operates the database through steps to register a user
Let’s make the simplest registration page. There are three parameters in the registration page:
1. Username
2. Password
3. Repeat password
The user must write three After the parameters, when you click submit, pass in the POST record 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:
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.
The visible data are:
Variables | Description |
---|---|
$_POST['username'] | Username |
$_POST['password'] | Password |
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 include:
Variables | Description |
---|---|
$time | User’s registration time |
$_SERVER['REMOTE_ADDR'] | User’s registration IP |
1. The unix timestamp returned by time
2.REMOTE_ADDR returns 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.
//连接数据库 $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 got the username, 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):
id | username | password | createtime | createip |
---|---|---|---|---|
User number | Username | Password | Creation time | Create IP |
five , send statements and determine status
mysqli_query As we said above, two parameters need to be passed in:
The connected resource, 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);
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: