PHP 개발 기업 웹사이트 ...로그인

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>

위 코드를 보면 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>
코스웨어