As we mentioned in the previous course, click the login button and the form is submitted to main.php
Let’s take a look at the specific content of the following main.php file
First we put The file to connect to the database is introduced, that is, the conn.php file
require_once("conn.php"); //Introduce the file to connect to the database
Form submission The way is to submit it by post
So we need to get the content of the form
$name=$_POST['username'];
$password=$_POST[ 'password'];
The two variables are used to store the values received in the post method
Let's first look at the login flow chart:
Next we have to think about login, under what circumstances the login is successful
When the information submitted by our form exists in the database table, then we can log in. If there is no such user, then Can't log in
So we write the query statement
$sql = "select * from user where username='$name' and password='$password'";
Then execute the sql statement
$info = mysql_query($sql);
In this way, we have queried the result. Through the mysql_fetch_row function, we obtain a row from the result set as a numeric array
$row = mysql_fetch_row($info);
Then we have to judge $row. If it is queried, the login is successful, otherwise it is a failure;
The complete code is as follows:
<?php require_once("conn.php");//首先链接数据库 $name=$_POST['username']; $password=$_POST['password']; $sql = "select * from user where username='$name' and password='$password'"; $info = mysql_query($sql); $row = mysql_fetch_row($info); if($row){ echo "<script>alert('登录成功')</script>"; }else{ echo "<script>alert('登录失败')</script>"; //echo "<script>history.go(-1);</script>"; //登录失败返回上一个页面 echo "<script>location.href='login.php';</script>"; //登录失败,跳转到另外一个页面 } ?>Next Section