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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
