2020-09-21---php&mysql实现登录注册

环境

在这里插入图片描述

数据库

在这里插入图片描述

文件结构

在这里插入图片描述
必须要注意,文件夹必须放在服务器上,然后才能运行。

步骤

在服务器上新建一users个文件夹,然后新建login.html和registor.html文件,接着新建一个php文件夹,用来放php文件,在php文件夹里面,新建login.php和registor.php。最后在新建一个API文档apidoc.md,用来写一些解释说明,后端提供给前端的一些数据说明。

代码

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>login</title>
</head>
<body>
    <h3>用户登录</h3>
    <form action="php/login.php" method="get">
        <p>请输入用户名:<input type="text" name="uname"></input></p>
        <p>请输入密码:<input type="password" name="upwd"></input></p>
        <p><input type="submit" value="登录"></p>
    </form>
</body>
</html>

registor.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>registor</title>
</head>
<body>
    <h3>用户注册</h3>
    <form action="php/registor.php" method="get">
        <p>请输入用户名:<input type="text" name="uname"></input></p>
        <p>请输入密码:<input type="password" name="upwd"></input></p>
        <p><input type="submit" value="注册"></p>
    </form>
</body>
</html>

login.php

<?php
    //1.设置字符集
    header('content-type:text/html;charset=utf-8');
    //2.接受前端请求的数据
    $uname = $_GET['uname'];
    $upwd = $_GET['upwd'];
    //3.将接受的数据存入数据库
    //(1)连接并选择要操作的数据库
    $db = mysqli_connect('localhost','root','root','day20200921');
    //(2)编写sql语句
    $sql = "select * from users where uname ='$uname'";
    //(3)执行sql语句
    $set = mysqli_query($db,$sql);
    //将集合转为数组
    $arr = mysqli_fetch_array($set);
    //判断数据库中是否有该用户
    if($uname == $arr['uname']){
        if($upwd == $arr['upwd']){
            echo "<script>alert('登录成功!');location.href='../login.html';</script>";
        }else{
            echo "<script>alert('登录失败,密码错误!');location.href='../login.html';</script>";
        }
    }else{
        echo "<script>alert('登录失败,用户名不存在!');location.href='../login.html';</script>";
    }
    

registor.php

<?php
    //1. 设置字符集
    header('content-type:text/html;charset=utf-8');
    //2. 接收前端请示的参数
    //用户名
    $uname = $_GET['uname'];
    //密码
    $upwd = $_GET['upwd'];

    // echo $uname,$upwd;
    //3. 将用户名和密码存入数据库
    //操作数据库
    //1. 连接并选择操作的数据库
    $db = mysqli_connect('localhost','root','root','day20200921');
    //第一个参数:连接数据库的url地址
    //第二个参数:登录数据库的用户名
    //第三个参数:登录数据库的密码
    //第四个参数:选择操作的数据库
    
    //2. 编写sql语句(查)
    //查表中是否已经有要注册的这个用户名
    $sql = "select * from users where uname='$uname'"; //查操作结果返回一个集合
    //3. 执行sql语句
    $set = mysqli_query($db,$sql);//要在$db数据库中执行$sql语句  //返回集合
    //将集合转为数组 mysqli_fetch_array() : 一次只能转一条记录。
    $arr = mysqli_fetch_array($set);
  
    //判断用户名是否存在
    // $arr['uname']   关联数组,key(数据库中的字段uname)对应的value
    //前端接受回来的变量  数据库中的字段
    if($uname == $arr['uname']){
        echo "<script>alert('用户名已存在!');location.href= '../registor.html';</script>";
    }else{
        $sql = "insert into users (uname,upwd) values ('$uname','$upwd')";
        $row = mysqli_query($db,$sql);
        if($row){
            echo "<script>alert('注册成功!');location.href='../login.html';</script>";
        }else{
            echo "<script>alert('注册失败!');location.href='../registor.html';</script>";
        }
    }
   


apidoc.md

用户注册
method:get
url : php/registor.php
参数
    1. uname String 用户名
    2. upwd String  用户密码


用户登录
method:get
url : php/login.php
参数
    1. uname String 用户名
    2. upwd String 用户密码

上面的URL就是表单提交时的action值,method就是表单里的post值。
uname就是input输入框填写用户名的name属性值,upwd就是input输入框添加密码的name属性值。
这些都必须一致。

效果

在这里插入图片描述
在这里插入图片描述
点击确定,跳转到登录界面
在这里插入图片描述
在这里插入图片描述
当用户名或密码错误时会有相应的提示
在这里插入图片描述
在这里插入图片描述
最需要注意的就是路径问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值