PHP写一个登录表单,并实现注册+登录功能

要求:

1、表单必须与数据库交互
2、注册和登录功能都必须有

登录html代码:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="GBK" />
        <title>表单验证作业</title>
    </head>
    <body>
        <form action="a.php">
            <center>
                账号:<input type="text" name="username" /><br />
                密码:<input type="password" name="password" /><br />
                <input type="submit" value="登录" /><br />
                <a href="b.html">注册</a>
            </center>
        </form>
    </body>
</html>

注册html代码:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="GBK">
        <title>注册</title>
    </head>
    <body>
        <form action="c.php" method="post">
            <center>
                账号:<input type="text" name="username" /><br />
                密码:<input type="password" name="password" /><br />
                确认密码:<input type="password" name="password2" /><br />
                <input type="submit" value="注册" />
            </center>
        </form>
    </body>
</html>

登录php代码:

<?php
header(‘content-type: text/html; charset=GBK’);
@$username=$_POST[‘username’];
@$password=$_POST[‘password’];
$conn = mysqli_connect(“127.0.0.1”,”root”,”root”,”sem”);//连接数据库
$sql =” select * from admin where username=’$username’ and password=’$password’”;
$result = mysqli_query($conn,$sql);
$s = mysqli_fetch_array($result);
if($s[‘username’] == $username and $s[‘password’] == $password){
echo ‘登陆成功’;
}else{
echo ‘登陆失败’;
}
?>

注册php代码:

<?php
header(‘content-type: text/html; charset=GBK’);
@$username=$_POST[‘username’];
@$password=$_POST[‘password’];
@$password2=$_POST[‘password2’];
if($password != $password2)
echo “<script>alert(‘两次输入的密码不匹配,请重新输入’);window.location.href=’b.html’</script>“;
$conn = mysqli_connect(“127.0.0.1”,”root”,”root”,”sem”); //连接数据库
$sql2 =” insert into admin(username,password) value(‘$username’,’$password’)”; //插入数据
$result = mysqli_query($conn,$sql2);

$s = $result;
if($s[‘username’] == $username and $s[‘password’] == $password){
echo ‘该账户已存在’;
}else{
echo ‘注册成功’;

}
?>

注意:访问登录和注册页面不应使用绝对路径,应该使用相对路径127.0.0.1访问

登录和注册页面如下:

 

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米兔-miny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值