完成使用Medoo框架完成一个小型的用户管理系统

Original 2019-05-09 17:10:15 287
abstract:<?php //index.php include "config.php"; include "operate.php"; //foreach ($res as $row){ //    echo print_r($row,true),'<
<?php
//index.php
include "config.php";
include "operate.php";
//foreach ($res as $row){
//    echo print_r($row,true),'<hr>';
//}
?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户管理系统</title>
    <style>
        .left_table{
            width: 50%;
            float: left;
            background: aliceblue;
        }
        .table_form a{
            background: #79E8F0;
            padding: 20px;
            text-decoration: none;
            float: left;
           margin: 100px 30px;
        }
        .user_table{
            width: 100%;
            margin: 50px auto;
            text-align: center;
            border-collapse: collapse;
            padding:2px;
        }
        caption{
            font-size: 30px;
        }
        .user_table th {
            border: 1px solid pink;
            padding: 5px;
        }
        .user_table td{
            text-align: center;
            border: 1px solid pink;
            padding: 5px;
        }
        .user_table td a{
            text-decoration: none;
            background: lightblue;
            padding: 2px 8px;
            font-size: 9px;
        }
     #add_user,#find_user,#mod{
         background: pink;
         float: right;
         width: 50%;
         height: 100px;
         border-radius: 5px;
         display: none;
     }
      #add_user input{
          margin-top: 15px;
          margin-left: 15px;
      }
     #find_user input{
          margin-top: 15px;
          margin-left: 15px;
      }

      #mod input{
            margin-top: 15px;
            margin-left: 15px;
        }

        .user_table span a{
            text-decoration: none;
        }

        .root_span a{
            text-decoration: none;
            font-size: 20px;
            background: greenyellow;
            padding: 10px;
            float: left;
            margin-top: 20px;
            margin-left: 20px;
        }
     </style>
</head>
<body>

<div>
    <table >
        <span>
            <a href="#" id="button_add">增加新用户</a>
            <a href="#" id="button_find">查找用户</a>
            <a href="index.php">返回</a>
        </span>
        <caption ><?php echo "用户管理系统"?></caption>
        <tr>
            <th><?php echo "工号";?></th>
            <th><?php echo "姓名";?></th>
            <th><?php echo "联系电话";?></th>
            <th><?php echo "国籍";?></th>
            <th><?php echo "出生日期";?></th>
            <th><?php echo "体重";?></th>
            <th><?php echo "身高";?></th>
            <th><?php echo "入职日期";?></th>
            <th><?php echo "操作";?></th>
        </tr>

        <?php foreach ($res as $row):?>
        <tr>
            <td><?php echo $row['uid'];?></td>
            <td><?php echo $row['name'];?></td>
            <td><?php echo $row['phone'];?></td>
            <td><?php echo $row['country'];?></td>
            <td><?php echo $row['birthday'];?></td>
            <td><?php echo $row['weight'];?></td>
            <td><?php echo $row['height'];?></td>
            <td><?php echo $row['add_time'];?></td>
            <td><a href="#" id="mod_user" onclick="mod(<?php echo $row['uid'];?>)">修改</a>
                <a href="#" onclick="del(<?php echo $row['uid'];?>)">删除</a></td>
        </tr>
     <?php endforeach;?>
    </table>
</div>


<div id="add_user">
    <form action="index.php" method="post">
        <input type="text" name="user_name" placeholder="姓名">
        <input type="text" name="phone" placeholder="联系电话">
        <input type="text" name="country" placeholder="国籍">
        <input type="text" name="birthday" placeholder="出生日期"><br>
        <input type="text" name="weight" placeholder="体重">
        <input type="text" name="height" placeholder="身高">
        <input type="text" name="add_time" placeholder="入职日期">
        <input type="hidden" name="act" value="add">
        <input type="submit" value="确定添加" style="background: #79E8F0;padding: 5px;border: 0px;border-radius: 5px;margin-left: 15px">
        <input type="reset" value="清空" style="background: #79E8F0;padding: 5px;border: 0px;border-radius: 5px;margin-left: 15px">
    </form>
</div>

<div id="find_user">
    <form action="index.php" method="post" style="margin: 15px">
        <input type="hidden" name="act" value="find">
        <select name="field">
            <option value="name">姓名</option>
            <option value="phone">联系电话</option>
            <option value="country">国籍</option>
            <option value="birthday">出生日期</option>
            <option value="weight">体重</option>
            <option value="height">身高</option>
            <option value="add_time">入职日期</option>
        </select>

        <select name="find_where">
            <option value="0"> 不等于 </option>
            <option value="1">  等于 </option>
            <option value="2"> 小于 </option>
            <option value="3"> 大于 </option>

        </select>
        <input type="text" name="find_val" placeholder="请输入要查找的值">
        <input type="submit" value="确定查找" style="background: #79E8F0;padding: 5px;border: 0px;border-radius: 5px;margin-left: 15px">
        <input type="reset" value="清空" style="background: #79E8F0;padding: 5px;border: 0px;border-radius: 5px;margin-left: 15px">
    </form>
</div>
</div>

<script>
    document.getElementById("button_add").onclick=function () {
        document.getElementById("add_user").style.display='block';
        document.getElementById("find_user").style.display='none';
        document.getElementById("mod").style.display='none';
    }
    document.getElementById("button_find").onclick=function () {
        document.getElementById("find_user").style.display='block';
        document.getElementById("add_user").style.display='none';
        document.getElementById("mod").style.display='none';
    }

    function mod(uid) {
        location.href="index.php?act=mod&id="+uid;
    }
    function del(uid) {
        if(confirm('是否删除本条记录?'))
        {
            location.href="index.php?act=del&id="+uid;
        }
    }
</script>
</body>
</html>
<?php
/**
 * 操作方法文件operate.php
 */

function alertMes($mes,$url)
{
    echo "<script>alert('{$mes}'); location.href='{$url}'</script>";
}

if($act=="del")
{
    $where=['uid'=>$id];
  $db->delete($data,$where);
    $mes="删除".$id."操作成功";
    alertMes($mes,$url);
}elseif($act=="mod"){
    $mod_arr=$db->get($data,$arr,['uid'=>$id]);
    $mod_add_time=date("Y-m-d",$mod_arr['add_time']);
    $str =<<<HERE
<div id="mod" style="float: right;display: block;">
    <form action="index.php?act=doMod&id={$id}" method="post">
        <input type="text" name="user_name" placeholder="姓名" value="{$mod_arr['name']}">
        <input type="text" name="phone" placeholder="联系电话" value="{$mod_arr['phone']}">
        <input type="text" name="country" placeholder="国籍" value="{$mod_arr['country']}">
        <input type="text" name="birthday" placeholder="出生日期" value="{$mod_arr['birthday']}"><br>
        <input type="text" name="weight" placeholder="体重" value="{$mod_arr['weight']}">
        <input type="text" name="height" placeholder="身高" value="{$mod_arr['height']}">
        <input type="text" name="add_time" placeholder="入职日期" value="{$mod_add_time}">        
        <input type="submit" value="确定修改" style="background: #79E8F0;padding: 5px;border: 0px;border-radius: 5px;margin-left: 15px">
        <input type="reset" value="清空" style="background: #79E8F0;padding: 5px;border: 0px;border-radius: 5px;margin-left: 15px">
    </form>
</div>  
HERE;
 echo $str;
}elseif($act=="doMod"){
    $mod_arr=['name'=>$_POST['user_name'],
        'phone'=>$_POST['phone'],
        'country'=>$_POST['country'],
        'birthday'=>$_POST['birthday'],
        'weight'=>$_POST['weight'],
        'height'=>$_POST['height'],
        'add_time'=>strtotime($_POST['add_time'])];
    $db->update($data,$mod_arr,['uid'=>$id]);

    $mes="成功更新第".$id."条记录";
    alertMes($mes,$url);
}elseif ($act=="add"){
    $add_arr=['name'=>$_POST['user_name'],
        'phone'=>$_POST['phone'],
        'country'=>$_POST['country'],
        'birthday'=>$_POST['birthday'],
        'weight'=>$_POST['weight'],
        'height'=>$_POST['height'],
        'add_time'=>strtotime($_POST['add_time'])];
    $stmt=$db->insert($data,$add_arr);
     $mes= "成功添加条".$stmt->rowCount()."记录";
    alertMes($mes,$url);
}elseif($act=="find"){
   $find_field=$_POST['field'];
   $find_where=$_POST['find_where'];
   $find_val=$_POST['find_val'];

    switch ($find_where)
    {
//        不等于
        case 0:
            $str=$find_field."[!]";
            $res = $db->select($data,$arr,[$str=>$find_val]);
            break;
//            等于
        case 1:
            $res = $db->select($data,$arr,[$find_field=>$find_val]);
            break;
//            小于
        case 2:
            $str=$find_field."[<]";
            $res = $db->select($data,$arr,[$str=>$find_val]);
            break;
//            大于
        case 3:
            $str=$find_field."[>]";
            $res = $db->select($data,$arr,[$str=>$find_val]);
            break;
    }

}
<?php
require __DIR__.'/Medoo/src/Medoo.php';

use Medoo\Medoo as Db;

$config=[
    'database_type' => 'mysql',
    'database_name' => 'php_io',
    'server' => '127.0.0.1',
    'username' => 'root',
    'password' => 'root',
    // [optional]
    'charset' => 'utf8',
    'port' => 3306,
];
$db=new Db($config);
$data = 'user';
$arr=['uid','name','phone','country','birthday','weight','height','add_time'];
$res = $db->select($data,$arr,null);

//操作名称
@$act=$_REQUEST['act'];
@$id=$_REQUEST['id'];

//sql字段
@$act_row=$_REQUEST['act_row'];
//条件
@$act_where=$_REQUEST['act_where'];

$url="index.php";
?>

20190509165656.jpg

20190509165817.jpg

20190509165847.jpgx经过本章节掌握了数据库数据的操和前端遍历显示,表单数据提交并接收后写入数据库中等等综合技能

Correcting teacher:查无此人Correction time:2019-05-10 14:22:37
Teacher's summary:完成的不错。简单的框架,有大作用。继续加油。

Release Notes

Popular Entries