基础版的后台管理员管理

来源:互联网 发布:冒险岛游戏数据库系统 编辑:程序博客网 时间:2024/05/19 01:10

自己大学时的一个作业,既然做出来了,就保存下,以后慢慢多学多完善.

1.创建数据库:

1.1新建一个Admin.sql数据库:

/*创建myadmin数据库*/
CREATE DATABASE myadmin;
/*创建表*/
CREATE TABLE Admin( id int auto_increment PRIMARY KEY not NULL ,
name VARCHAR (20) not null,
password VARCHAR (12) not null
);
/*第一个管理员*/
INSERT INTO admin(name,password)VALUES ('王彬','123456')
1.2链接数据库创建一个conn.php来链接数据库
<?php$conn=mysqli_connect("localhost",'root','123456','myadmin' );if(mysqli_error($conn)){    mysqli_errno($conn);    exit;}mysqli_set_charset($conn,"utf8");?>

2.创建一个登录界面login.html(界面很丑,自己忍一忍就过去了)

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>后台登录界面</title></head><body>    <h2>欢迎进入B2C商城后台管理系统</h2>    <form method="post"  action="index.php">        用户名:<input type="text" name="name"/><br>        密码:<input type="password" name="password"/><br>            <input type="submit" name="submit" value="登录"/><br>    </form></body></html>
3.登陆后进入index.php进行验证一下:
<?php//接收提交的数据echo'<meta http-equiv="CONTENT-TYPE" content="text/html"  charset="utf-8">';if(isset($_POST['submit'])&&$_POST['submit']=="登录"){    $name=$_POST['name'];    $psd=$_POST['password'];    if($name==''||$psd==''){        echo"账户密码不得为空";    }else{        include "conn.php";        $sql="select name,password from Admin where name='$name' AND  password='$psd'";        $result=mysqli_query($conn,$sql);      if($row=mysqli_fetch_array($result)){          echo"登录成功 </br>";          echo "<a href='lis.php'> 管理员列表</a> </br>";          echo "<a href='add.php'> 添加管理员</a> </br>";          }else{          echo"用户密码不匹配";      }    }}else{    echo"登陆失败";}
4.验证无误后进行数据的增删改查操作
4.1:lis.php:
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>后台管理系统</title>    <script>        function doDel(id){            if(confirm('确认删除')){                window.location='action.php?action=del&id='+id;            }        }    </script></head><body><h3>浏览管理员信息</h3><table width="350" border="2" cellpadding="0">    <tr>        <th>ID</th>        <th>姓名</th>        <th>密码</th>        <th>操作</th>    </tr>    <?php    //链接数据库    header("content-type:text/html;charset=utf8");    include "conn.php";    mysqli_set_charset($conn,'utf8');    //执行sql    $sql_select="select * from Admin";    //data的解析    //注意注意    $rs=mysqli_query($conn,$sql_select);    while ($row=mysqli_fetch_array($rs)) {        echo "<tr>";        echo "<th>{$row['id']} </th>";        echo "<th>{$row['name']}</th>";        echo "<th>{$row['password']} </th>";        echo "<td>          <a href='edit.php?id={$row['id']}'>修改</a>          <a href='javascript:void(0);' onclick='doDel({$row['id']})'>删除</a>        </td>";        echo "</tr>";    }    ?></table></body></html>
4.2:add.php:
<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>添加管理员</title></head><body><?php /*include "menu.html";*/?><h3>添加学生</h3><form action="action.php?action=add" method="post">    <table>        <tr>            <td>用户名</td>            <td><input type="text" name="name"></td>        </tr>        <tr>            <td>密码</td>            <td><input type="text" name="password"></td>        </tr>        <tr>            <td><a href="index.php">返回</td>            <td><input type="submit" value="添加"></td>        </tr>    </table></form></body></html>
4.3:edit.php
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>管理员管理系统</title></head><body><?php /*include('menu.html');*///连接数据库header("content-type:text/html;charset=utf8");include "conn.php";mysqli_set_charset($conn,'utf8');$id=$_GET['id'];//执行sql语句$sql_select="select * from admin where id='$id'";$stmt=mysqli_query($conn,$sql_select);if($stmt){    //解析数据    $stu=mysqli_fetch_assoc($stmt);}else{    die("没有这个id{$_GET['id']}");}?><h3>修改管理员信息</h3><form action="action.php?action=edit" method="post">    <input type="hidden" name="id" value="<?php echo$stu['id'];?>">    <table>        <tr>            <td>姓名</td>            <td><input type="text" name="name" value="<?php echo $stu['name'];?>"></td>        </tr>        <tr>            <td>密码</td>            <td><input type="text" name="password" value="<?php echo $stu['password'];?>"></td>        </tr>        <td><input type="submit" value="修改"></td>        <td><input type="submit" value="重置"></td>    </table></form></body></html>
4.4:action.php(这里是吧所有的方法放入了一个文件里):
<?phpheader("content-type:text/html;charset=utf8");include "conn.php";mysqli_set_charset($conn,'utf8');if($conn){    switch($_GET['action']){        case 'add':            $name=$_POST['name'];            $password=$_POST['password'];            $sql="insert into admin(name,password)VALUES ('$name','$password')";            $rw=mysqli_query($conn,$sql);            if($rw>0){                echo "<script>alert('添加成功');window.location.href='lis.php'</script>";            }else{                echo"<script>alert('添加失败');window.location.href='lis.php'</script>";            }            break;        case 'del':            $id=$_GET['id'];            $sql="delete from admin where id='$id'";            $rw=mysqli_query($conn,$sql);            if($rw>0){                echo "<script>alert('删除成功');window.location.href='lis.php'</script>";            }else{                echo"<script>alert('删除失败');window.location.href='lis.php'</script>";            }            break;        case'edit':            $id=$_POST['id'];            $name=$_POST['name'];            $password=$_POST['password'];            $sql="update admin set name='$name',password='$password' WHERE id='$id'";            $rw=mysqli_query($conn,$sql);            if($rw>0){                echo"<script>alert('修改成功');window.location.href='lis.php';</script>";            }else {                echo "<script>alert('修改失败');window.location.href='lis.php'</script>";            }            header('Location;lis.php');            break;    }}else{    die('数据库连接失败'.mysqli_connect_error());}?>



原创粉丝点击