Registration function (2)
The front-end page of the registration page has been completed. Let’s talk about the background program code.
First of all, we need to know that the registration function is actually the process of adding data to the database. To add data to the database, you must first connect to the database. There is no doubt about this. Then, you must obtain the registration information passed from the front-end page in the background. We only have user name and password here. You can add them as needed in actual projects in the future. Database fields. After obtaining the value passed by the form, use the SQL statement to write an add statement to add the obtained value to the database. In this way, our entire registration process is almost complete. Let's take a closer look at the code.
Step 1: Connect to the database
<?php header("content-type:text/html;charset=utf-8"); //连接数据库 $link = mysqli_connect("localhost","root","root","joke"); if (!$link) { die("连接失败: " . mysqli_connect_error()); }
Step 2: Get the value passed by the form
<?php $username=$_POST['username']; $password=$_POST['password']; ?>
Let me tell you here that the registration function does not allow direct submission without filling in the value Yes, it cannot be empty and must be filled, so
Step 3: Verify that the information is complete and write the insertion statement:
<?php if($username == "" || $password == "") //判断前端页面传递的值是不是完整 { echo "请确认信息完整性"; }else{ $sql="insert into login(username,password) values('$username','$password')"; //完整的话讲传递过来的数据插入数据库 $result=mysqli_query($link,$sql); //执行操作,将返回的结果赋值给变量$result if(!$result) //判断$result有没有值,如果有就添加成功,跳转至登录页面;如果没有值,说明添加失败,返回注册页面 { echo"注册不成功!"."<br/><br/>"; echo"<a href='resgiter.html'>返回</a>"; } else { echo"注册成功!"."<br/><br/>"; echo"<a href='login.html'>立刻登录</a>"; } }
The above are the steps to register the function.