美文网首页php学习
php实战一个用户列表

php实战一个用户列表

作者: 半数的年 | 来源:发表于2018-07-14 11:32 被阅读0次

    先看看页面效果
    1、用户列表界面


    image.png

    2、添加用户界面


    image.png
    3、编辑用户界面
    image.png

    工具:phpStorm + wampServer + chrome浏览器

    1、用户列表界面代码userList.php 先查询用户结果集 再将之展示

    <?php
    /**
     * Created by PhpStorm.
     * User: zhengjiayuan
     * Date: 2018/7/12
     * Time: 17:57
     */
    
    $mysqli = new mysqli('localhost','root','','article');
    if ($mysqli->connect_errno){
        // 错误数 > 0 输出错
        die('CONNECT ERROR:'.$mysqli->connect_error);
    }
    $sql = "SELECT id,username,age from user ";
    $mysqli_result = $mysqli->query($sql);
    if ($mysqli_result && $mysqli_result->num_rows > 0){
        // 查询结果条数 > 0
        // 将每一行 一行一行 放进 rows
        while ($row = $mysqli_result->fetch_assoc()){
            $rows[]  = $row;
        }
    
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>文章发布系统</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link href="default.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <h2>用户列表-<a href="addUser.php">添加用户</a></h2>
        <table border="1" cellpadding="0" width="80%" bgcolor="#ABCDEF">
            <tr><tr>
                <td>编号</td>
                <td>用户名</td>
                <td>年龄</td>
                <td>操作</td>
            </tr>
            <!-- 先一行一行取出 放进对应的属性 -->
            <?php $i=1;foreach ($rows as $row):?>
                <tr>
                    <td><?php echo $i++;?></td>
                    <td><?php echo $row['username'];?></td>
                    <td><?php echo $row['age'];?></td>
                    <td>
                        <a href="editUser.php?act=ediUser&id=<?php echo $row['id']?>">更新</a>|
                        <a href="doAction.php?act=delUser&id=<?php echo $row['id']?>">删除</a>
                    </td>
                </tr>
            <?php endforeach;?>
        </table>
    </body>
    </html>
    
    

    2、添加用户界面addUser.php 这个主要前端代码 添加用户请求给doAction.php

    <?php
    /**
     * Created by PhpStorm.
     * User: zhengjiayuan
     * Date: 2018/7/12
     * Time: 18:27
     */
    ?>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>文章发布系统</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link href="default.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <h2>添加用户</h2>
        <form action="doAction.php?act=addUser" method="post">
            <table border="1" cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="80%">
                <tr>
                    <td>用户名</td>
                    <td><input type="text" name="username" id="" placeholder="请输入合法用户名" required="required"/></td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td><input type="password" name="password" id="" placeholder="请输入密码" required="required"/></td>
                </tr>
                <tr>
                    <td>年龄</td>
                    <td><input type="number" name="age" id="" min="1" max="125" placeholder="请输入合法年龄名" required="required"/></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="添加用户" /></td>
                </tr>
            </table>
        </form>
    </body>
    </html>
    

    3、处理请求代码 doAction.php 处理添加用户、更新用户、删除用户

    <?php
    /**
     * Created by PhpStorm.
     * User: zhengjiayuan
     * Date: 2018/7/12
     * Time: 18:36
     */
        header('content-type:text/html;charset=utf-8');
        $mysqli = new mysqli('localhost','root','','article');
        if ($mysqli->connect_errno){
            // 错误数 > 0 输出错误
            die($mysqli->connect_error);
        }
        // 设置数据库编码
        $mysqli->set_charset("utf8");
    
        $username = $_POST['username'];
        $username = $mysqli->escape_string($username);  // 转义用户输入的单引号 或者 双引号
        $password = md5($_POST['password']);
        $age = $_POST['age'];
        // 请求方式 添加或者更新或删除
        $act = $_GET['act'];
    
        $id = $_GET['id'];
        switch ($act){
            case 'addUser':
                // 添加用户
                $sql = "insert into user(username,age) values ('$username','$age')";
                $res = $mysqli->query($sql);
                if ($res){
                    echo "<script type='text/javascript'>alert('添加用户成功');
                            location.href='userList.php';
                          </script>";
                }else{
                    echo "<script type='text/javascript'>alert('添加用户失败,重新添加');
                            location.href='addUser.php';
                          </script>";
                }
                break;
            case 'delUser':
                // 删除用户
                $sql = "delete from user where id = ".$id;
                $res = $mysqli->query($sql);
                if ($res){
                    $mes = "删除成功";
                }else{
                    $mes = "删除失败";
                }
                $usel = 'userList.php';
                echo "<script type='text/javascript'>alert('$mes');
                            location.href='$usel';
                          </script>";
                break;
            case 'editUser':
                // 编辑用户
                $sql = "update user set username = '$username',age = '$age' where id = '$id'";
                $res = $mysqli->query($sql);
                if ($res){
                    $mes = "更新成功";
                }else{
                    $mes = "更新失败";
                }
                $usel = 'userList.php';
                echo "<script type='text/javascript'>alert('$mes');
                            location.href='$usel';
                          </script>";
                break;
        }
    

    4、更新用户界面 editUser.php 根据传参过来的id先查询用户信息进行展示,再将用户的修改请求转发doAction.php进行展示

    <?php
    /**
     * Created by PhpStorm.
     * User: zhengjiayuan
     * Date: 2018/7/12
     * Time: 18:27
     */
    
    header('content-type:text/html;charset=utf-8');
    $mysqli = new mysqli('localhost','root','','article');
    if ($mysqli->connect_errno){
        // 错误数 > 0 输出错误
        die($mysqli->connect_error);
    }
    $mysqli->set_charset("utf8");
    $id = $_GET['id'];
    // 获取对应id用户的信息
    $sql = "select id,username,age from user where id = ".$id;
    $mysqli_result = $mysqli->query($sql);
    if ($mysqli_result && $mysqli_result->num_rows>0){
        $row = $mysqli_result->fetch_assoc();
    }
    ?>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>文章发布系统</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link href="default.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <h2>编辑用户</h2>
    <form action="doAction.php?act=editUser&id=<?php echo $row['id']?>" method="post">
        <table border="1" cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="80%">
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username" id="" placeholder="请输入合法用户名" required="required" value="<?php echo $row['username']?>"/></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password" id="" placeholder="请输入密码" required="required" /></td>
            </tr>
            <tr>
                <td>年龄</td>
                <td><input type="number" name="age" id="" min="1" max="125" placeholder="请输入合法年龄名" required="required" value="<?php echo $row['age']?>"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="更新用户" /></td>
            </tr>
        </table>
    </form>
    </body>
    </html>
    

    相关文章

      网友评论

        本文标题:php实战一个用户列表

        本文链接:https://www.haomeiwen.com/subject/bgnepftx.html