How to connect php to mysql to implement a simple registration login page
php mysql tutorial column introduces the implementation of a simple registration login page.
Recommended: php myql tutorial
Login page index.html
Registration page register.html
After installing the Mysql database, use the MySQL database management page phpmyadmin creates database and form
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>") ?>
————————————————————————————————————
Supplement: (pits that have been stepped on)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

