ymx-jiaoyang 2022-09-24 13:27 采纳率: 33.3%
浏览 94
已结题

php+mysql登录注册验证

使用mysql+php做简单登录注册跳转验证的错误,希望来个明白的朋友指点一下如何改正

最后两张第一张是登录成功后该有的简单页面
第二张是退出登录直接到登录页面的代码


```login.php的代码从下面开始

<?php
session_start();//开启Session的使用来保存使用

?>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="all">
        <div class="login-all">
            <ul class="title">
                <li>世间美好,与你相拥</li>
            </ul>

            <div class="form_div">
                <form id="form1" name="form1" method="post" action="#" >
                    <table class="tb">
                        <tr>
                            <td ><input type="text" name="用户名" id="textfield" placeholder="请输入您的用户名"/></td>
                        </tr>
                        <tr>
                            <td><input type="text" name="密码" id="textfield2" placeholder="请输入您的密码"/></td>
                        </tr>
                        <tr>
                            <td colspan="2" align="center" class="tb-button">
                                <input type="submit" name="登录按钮" id="button" value="登录" />
                                <input type="button" name="注册" id="button2" value="注册" onclick="location.href='register.php'" />
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </div>
    <?php
    //在数据库中查询数据
        if ($_POST['登录按钮']  == '登录'){
            $con=mysqli_connect('localhost','root','123','antuyou',3306);

            mysqli_set_charset($con,"utf8");

            $x =$_POST["用户名"];
            $y =$_POST["密码"];
            $a=mysqli_query($con,"SELECT * FROM userinf WHERE name = '{$x}' AND password = '{$y}'");

            $row=mysqli_fetch_array($a);
            if (empty($row)){
                echo '用户名或密码为空';
            }else{
                echo '用户名和密码正确';
                $_SESSION['用户名'] =$_POST['用户名'];

                sleep(1);
                header('location:index.php');
            }

        }
    ?>


</body>
</html>



login.php的css,名字叫做style.css,从下面开始
*{
    list-style: none;
    padding: 0px;
    margin: 0px;
}

body{
    /*background: url("tree-736881.jpg");*/
    margin:0;
    background-size: 100% 100%;
    background-attachment: fixed;
}

.login-all{
    width: 450px;
    height: 350px;
    box-sizing: border-box;
    margin: 190px auto;              /*整个白框的外边距*/
    background: #bed8df;
    border-radius: 10px;
}
.login-all .title{
    font-size: 1em;
    font-family: 仿宋, serif;
    display: block;
    opacity: 0.8;
    padding: 0.2em;
    color: #000000;/*世间美好,与你相拥的设置*/
    font-size: 20px;
    padding-top: 25px;
    padding-left: 45px;            /*使他们在白框正中间*/
    padding-right: 100px;
    width: 332px;
    text-align: center;
    color: #bd7cf3;
}


.tb input{
    font-size: 1em;
    font-family: 仿宋, serif;
    display: block;
    opacity: 0.8;
    padding: 0.2em;
    color: #000000;/*这里设置的是表格input的整体高宽度*/
    width:300px;
    height:40px;
    margin:10px 100px 3px 70px;
    border-radius: 10px;
    box-sizing: border-box;
    border: 1px solid;
}




/*放上去会变成小手*/
#button{
    cursor: pointer;
}
#button2{
    cursor: pointer;
}


#form1{
    border-collapse: separate;
    border-radius: 10px;
}




```register.php的代码从下面开始

<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" type="text/css" href="style_register.css">
</head>
<body>
<div class="all_div">
    <div class="all_bg">
        <form id="form1" name="form1" method="post" action="#">
            <table class="tb">
                <tr class="input">

                    <td><input type="text" name="用户名" id="textfield" placeholder="请输入您想要的用户名"/></td>
                </tr>
                <tr class="input">

                    <td><input type="text" name="密码" id="textfield2" placeholder="请输入您的密码"/></td>
                </tr>

                <tr class="input">
                    <td><input type="email" name="邮箱" id="email1" placeholder="请输入您的enail地址" />
                    </td>
                </tr>
                <tr class="input">
                    <td><input type="text" name="电话" id="phone" placeholder="请输入您的联系方式"/></td>
                </tr>
                <tr class="tb_option">
                    <td><input type="text" name="下拉列表" id="option" list="sexlist" placeholder="请自定义你的性别">
                        <datalist id="sexlist">
                            <option></option>
                            <option></option>
                            <option>保密</option>
                        </datalist>


<!--                        <input type="radio" name="单选按钮" id="radio" />男-->
<!--                        <input type="radio" name="单选按钮" id="radio2" />女-->
<!--                        <input type="radio" name="单选按钮" id="radio2" />保密-->
                    </td>
                </tr>
                <tr class="input">
                    <td colspan="2" align="center"><input type="submit" name="注册按钮" id="button" value="注册" /></td>
                </tr>
            </table>
        </form>
    </div>
</div>
<?php
if ( @ $_POST["注册按钮"] == "注册"){
    $con=mysqli_connect('localhost','root','123','antuyou',3306);//连接数据库
    mysqli_set_charset($con,"utf8");
    $a=$_POST["用户名"];
    $b=$_POST["密码"];
    $c=$_POST["下拉列表"];
    $d=$_POST["邮箱"];
    $e=$_POST["电话"];


    mysqli_query($con,"insert into userinf(name,password,sex,email,phone) values('$a','$b','$c','$d','$e')");

    sleep(1);
    header('location:login.php');
}
?>







</body>
</html>




<?php
session_start();
?>
<html>
<head><title></title></head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<body>
<form id="form1" name="form1" method="post" action="#">
    <table width="400" border="1">
        <tr >
            <td width="229" height="84">用户名:    <?php echo $_SESSION['用户名'] ?>  </td>
            <td width="155"><input type="button" name="button" value="退出登录"     onclick="location.href='tuichu.php'"></td>
        </tr>
             <tr>
                <td></td>
                <td></td>
            </tr>



    </table>

</form>



</body>
</html>





<?php
session_start();
$_SESSION = array();
session_destroy();

sleep(1);
header('location:login.php');

?>
<html>
<head><title></title></head>
<body>

</body>
</html>


运行结果及报错内容

在输入账户和密码后,出现了如下错误
登录页面的错误

img


注册页面的错误

img


输入数据后页面不跳转(我写了跳转的记得)
我理想的是账户密码正确后直接跳转到我需要的index页面去,结果没有跳转
但是登录后应该跳转到的页面出现了我想要呈现的内容

img


退出页面则没什么大问题

img


数据库代码页

img

  • 写回答

4条回答 默认 最新

  • 三只小菜猿 PHP领域新星创作者 2022-09-24 14:36
    关注

    我将你得代码测试了,运行没有问题,应该是你配置得有问题环境

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月21日
  • 已采纳回答 10月13日
  • 修改了问题 9月24日
  • 修改了问题 9月24日
  • 展开全部

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算