Recommended: php myql tutorial
Login page index.html
Registration page register.html
Browser input: domain name/phpmyadmin, no domain name input: ip/phpmyadmin or 127.0.0.1/phpmyadmin (127.0.0.1 is the local address)
Create a form in the database
The code is as follows:
index.html
nbsp;html> <meta> <title>登录</title> <p> <b>登录</b> </p>
login.php
<meta><?php $conn = mysqli_connect('localhost','root','password','db_name') or die('数据库连接失败'); $conn->query("SET NAMES 'UTF8'"); $user = $_POST['user']; $pass = $_POST['pass']; $sql="SELECT * FROM users where user='{$user}' and pass='{$pass}'"; $result=$conn->query($sql); $row = mysqli_num_rows($result); //若表中存在输入的用户名和密码,row=1;若表中用户名不存在或密码错误,则row=0 if($row == 1){ echo $row['user']."登陆成功!"; } else{ echo"登录失败,请重新登录!"; } ?>
register.html
nbsp;html> <meta> <title>注册</title> <p> <b>注册</b> </p>
register. php
<?php header ( "Content-type:text/html;charset=utf-8" ); $conn = mysqli_connect('localhost','root','password','db_name') or die('数据库连接失败'); $conn->set_charset('utf8'); $user = $_POST['user']; $pass = $_POST['pass']; $connect = $_POST['connect']; $sql = "INSERT INTO users(id,user,pass,connect) VALUES (null,'{$user}' ,'{$pass}','{$connect}')"; mysqli_query($conn,$sql) or die(mysqli_error($conn)); echo("注册成功!!!<br><a>点击登录</a>") ?>
————————————————————————————————————
1. How to connect to the database with PHP7: using mysqli or PDO
When using mysqli_connect(), use localhost as the address It's not an IP, otherwise you won't be able to connect to the server.
Reference: https://blog.csdn.net/zwliang98/article/details/82997349
##2. php outputs the error message of executing the sql statement:
mysqli_query($conn,$sql) or die(mysqli_error( $conn ));
3. bug
bug 1:
Incorrect integer value: '' for column 'id' at row 1
For versions above php5, if the value is empty, NULL should be written
#错误 $sql = "INSERT INTO users(id,user,pass,connect) VALUES ('','{$user}' ,'{$pass}','{$connect}')";#改为 $sql = "INSERT INTO users(id,user,pass,connect) VALUES (null,'{$user}' ,'{$pass}','{$connect}')";
bug 2:
Incorrect string value: ' \xE7\x94\xB7' for column 'sex' at row 1
Link: MySQL insert Chinese error problem method
The above is the detailed content of How to connect php to mysql to implement a simple registration login page. For more information, please follow other related articles on the PHP Chinese website!