Home  >  Article  >  Topics  >  How to connect php to mysql to implement a simple registration login page

How to connect php to mysql to implement a simple registration login page

coldplay.xixi
coldplay.xixiforward
2020-11-25 17:33:248703browse

php mysql tutorial column introduces the implementation of a simple registration login page.

How to connect php to mysql to implement a simple registration login page

Recommended: php myql tutorial

Login page index.html
How to connect php to mysql to implement a simple registration login page
Registration page register.html
How to connect php to mysql to implement a simple registration login page

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)
How to connect php to mysql to implement a simple registration login page
Create a form in the database How to connect php to mysql to implement a simple registration login page

The code is as follows:

index.html

nbsp;html>
    <meta>
    <title>登录</title>
    <p>
        <b>登录</b>
        </p>
            

用户名:

            

密码:

            

 

        
        新用户注册     

login.php

<meta><?php     $conn = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;password&#39;,&#39;db_name&#39;) or die(&#39;数据库连接失败&#39;);
    $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(&#39;localhost&#39;,&#39;root&#39;,&#39;password&#39;,&#39;db_name&#39;) or die(&#39;数据库连接失败&#39;);
    $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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete