PHP开发企业网站教程之添加管理员

上节我们已经把信息从数据库取出,并展示了显示页面

从上节的代码中可以看到,我们点击添加管理员,提交到addu.php这个页面上

我们来看一下这个页面的代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加管理员</title>
    <style type="text/css">
        .ipt{width:180px;height:30px;border-radius:5px;
            outline:none;border:1px solid #eee;box-sizing:border-box;padding-left:15px;}
        .sub{width:50px;height:20px;border:1px solid #eee;background:#eee;color:#ff7575;}
    </style>
</head>
<body>
    <form method="post" action="adduser.php">
        用户名:<input type="username" name="username" class="ipt"></br></br>
        密&nbsp;码:<input type="password" name="password" class="ipt"></br></br>
        <input type="submit" value="添加" class="sub">
    </form>
</body>
</html>

看如上代码,可以看出,表单是以post 的方式提交到adduser.php 这个文件中

下面我们来看下adduser.php 这个文件的代码,然后分析下代码:

<?php
    //添加管理员部分代码,注意,当数据库存在该管理员账户时,不允许添加
    require_once('conn.php');
    $name = $_POST['username'];
    $password = md5($_POST['password']);

    $sql1 = "select * from user where username ='$name'";
    $info = mysql_query($sql1);
    $res1 = mysql_num_rows($info);
    if($res1){
        echo "<script>alert('管理员已存在');location.href='addu.php';</script>";
    }else{
        $sql  = "insert into `user`(username,password) values('$name','$password')";
        $res = mysql_query($sql);
        if($res){
            echo "<script>alert('添加管理员成功');location.href='user.php';</script>";
        }else{
            echo "<script>alert('添加管理员失败');history.go(-1);</script>";
        }
    }
?>

首先我们也是要链接数据库,然后接收表单提交过来的信息

之后我们要进行判断表单提交的用户是否存在,如果存在,给出提示,不存在,可以添加

如上代码所示,这样我们就完成了管理员添加的功能

继续学习
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>添加管理员</title> <style type="text/css"> .ipt{width:180px;height:30px;border-radius:5px; outline:none;border:1px solid #eee;box-sizing:border-box;padding-left:15px;} .sub{width:50px;height:20px;border:1px solid #eee;background:#eee;color:#ff7575;} </style> </head> <body> <form method="post" action="adduser.php"> 用户名:<input type="username" name="username" class="ipt"></br></br> 密 码:<input type="password" name="password" class="ipt"></br></br> <input type="submit" value="添加" class="sub"> </form> </body> </html>
提交重置代码
章节
笔记
提问
课件
反馈
捐赠

PHP开发企业网站实战教程