php如何连接mysql实现简单注册登陆页面-

php mysql教程

栏目介绍实现简单注册登陆页面。

推荐:php myql教程

登陆页面index.html



注册页面 register.html


安装Mysql数据库后,用MySQL数据库管理页面phpmyadmin创建数据库和表单

浏览器输入:

域名/phpmyadmin

,没有域名输入:

ip/phpmyadmin

或者

127.0.0.1/phpmyadmin

(127.0.0.1为本机地址)

在数据库里创建表单

代码如下:

index.html

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>登录</title></head><body>    <p>        <b>登录</b>        <form action="login.php" method="post">            <p><span>用户名:</span><input type="text" name="user" required="" /></p>            <p><span>密码:</span><input type="password" name="pass"/ required=""></p>            <p><input type="reset" name=""/> <input type="submit" name=""/></p>        </form>        <a href="register.html">新用户注册</a>    </p></body></html>

login.php

<meta charset="utf-8"><?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

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>注册</title></head><body>    <p>        <b>注册</b>        <form action="register.php" method="post">            <p><span>用户名:</span><input type="text" name="user" required=""/></p>            <p><span>密码:</span><input type="password" name="pass" required=""/></p>            <p><span>联系:</span><input type="text" name="connect" value="" required=""/></p>            <p>                <input type="reset" name=""/> <input type="submit" name=""/>            </p>        </form>        <a href="index.html">已注册</a>    </p></body></html>

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 href='index.html'>点击登录</a>")     ?>

———————————————————————————————————————

补充:(踩过的坑)

1. PHP7连接数据库的方式

:使用mysqli或PDO
使用mysqli_connect()时地址用localhost而不是ip,否则连接不上服务器。
参考:https://blog.csdn.net/zwliang98/article/details/82997349

2. php输出执行sql语句的错误信息:

mysqli_query($conn,$sql) or die(mysqli_error( $conn ));

3. bug


bug 1:


Incorrect integer value: ‘’ for column ‘id’ at row 1


php5以上的版本如果是空值应该要写NULL

#错误$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: ‘xE7x94xB7’ for column ‘sex’ at row 1


链接:MySQL插入中文错误问题方法

php如何连接mysql实现简单注册登陆页面-