index.php:
<?php require __DIR__.'/config/config.php'; $fileds=['id','name','sex','age','email','creat_time']; $table='user'; $rows=$db->select($table,$fileds); $smarty->assign('rows',$rows); $smarty->display('index.html');
index.html:
<!doctype html> <html lang="en"> <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> <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="tmp/css/style.css"> </head> <body> <div class="content"> <h1>江湖人士管理系统 <span style="float: right;margin-right: 20px;"> <a href="/medoo/add.php">增加英雄</a></span></h1> <table class="table table-bordered table-striped table-hover" cellpadding="0" cellspacing="0"> <thead> <tr> <th>id</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>邮箱</th> <th>注册时间</th> <th>操作</th> </tr> </thead> <tbody> {foreach $rows as $res} <tr> <td>{$res.id}</td> <td>{$res.name}</td> <td>{if $res.sex eq 0}男{else}女{/if}</td> <td>{$res.age}</td> <td>{$res.email}</td> <td>{$res.creat_time|date_format:"%Y-%m-%d"}</td> <td><a href="/medoo/edit.php?id={$res.id}">编辑</a> <a href="/medoo/del.php?id={$res.id}">删除</a></td> </tr> {foreachelse} <tr> <td colspan="7" style="color: red">暂无数据</td> </tr> {/foreach} <tr> <td colspan="7">当前共<span style="color:red;font-weight: bold">{count($rows)}</span>条数据</td> </tr> </tbody> </table> </div> </body> </html>
add.php:
<?php require __DIR__.'/config/config.php'; if($_POST){ $data[]=array(); $data=[ 'name'=>$_POST['name'], 'sex'=>$_POST['sex'], 'age'=>$_POST['age'], 'email'=>$_POST['email'], 'password'=>sha1($_POST['password']), 'creat_time'=>time() ]; if(!$stmt=$db->select('user','name',['name'=>$_POST['name']])){ $stmt=$db->insert('user',$data); if($stmt->rowCount()){ alert('添加成功','http://www.a.com/medoo/'); }else{ exit('添加失败'); } }else{ exit('用户已存在'); } } $smarty->display('add.html');
add.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="tmp/css/style.css"> </head> <body> <div class="edit"> <div class="position">当前位置: <a href="/medoo/">主页</a>>编辑</div> <h1>编辑</h1> <table class="table"> <form action="" method="post"> <input type="hidden" name="id"> <tr> <td>姓名</td> <td><input type="text" name="name"></td> </tr> <tr> <td>密码</td> <td><input type="text" name="password"></td> </tr> <tr> <td>性别</td> <td> <select name="sex"> <option value="0">男</option> <option value="1">女</option> </select> </td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age"></td> </tr> <tr> <td>邮箱</td> <td><input type="text" name="email"></td> </tr> <tr> <td colspan="2" style="background-color:transparent!important;"> <input type="submit" class="btn-success btn-lager" value="提交"> </td> </tr> </form> </table> </div> </body> </html>
edit.php:
<?php require __DIR__.'/config/config.php'; $id=$_GET['id']; $where=['id'=>$id]; $fileds=['id','name','sex','age','email']; $row=$db->get('user',$fileds,$where); if($_POST) { $data[] = array(); if($_POST['sex']!='女'){ $_POST['sex']=0; }else{ $_POST['sex']=1; } $data=[ 'name'=> $_POST['name'], 'sex'=> $_POST['sex'], 'age' => $_POST['age'], 'email'=> $_POST['email'], 'creat_time' => time() ]; $where=['id'=>$_POST['id']]; $table='user'; $stmt=$db->update($table,$data,$where); if($stmt->rowCount()){ alert("修改成功","http://www.a.com/medoo/"); }else{ exit('修改失败'.$stmt->errorInfo()); } } $smarty->assign('row',$row); $smarty->display('edit.html'
edit.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="tmp/css/style.css"> </head> <body> <div class="edit"> <div class="position">当前位置: <a href="/medoo/">主页</a>>编辑</div> <h1>编辑</h1> <table class="table"> <form action="" method="post"> <input type="hidden" name="id" value="{$row.id}"> <tr> <td>姓名</td> <td><input type="text" name="name" value="{$row.name}"></td> </tr> <tr> <td>性别</td> <td><input type="text" name="sex" value="{if $row.sex eq 0}男{else}女{/if}"></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" value="{$row.age}"></td> </tr> <tr> <td>邮箱</td> <td><input type="text" name="email" value="{$row.email}"></td> </tr> <tr> <td colspan="2" style="background-color:transparent!important;"> <input type="submit" class="btn-success btn-lager" value="提交"> </td> </tr> </form> </table> </div> </body> </html>
del.php:
<?php require __DIR__.'/config/config.php'; $stmt=$db->delete('user',['id'=>$_GET['id']]); if($stmt->rowCount()){ alert('删除成功','http://www.a.com/medoo/'); }else{ echo'删除失败'.$stmt->errorInfo(); }
config.php配置:
<?php //配置Smarty模版引擎 //如果是通过composer下载的配置如下: require __DIR__.'/database.php'; //实例化Smarty $smarty=new Smarty(); //配置四个目录:必选 //配置模板目录 $smarty->setTemplateDir(__DIR__.'/../tmp'); //编译文件所在目录 $smarty->setCompileDir(__DIR__.'/../tmp_c'); //缓存目录 $smarty->setCacheDir(__DIR__.'/../cache'); //配置目录 $smarty->setConfigDir(__DIR__.'/../config'); //可选配置 $smarty->setLeftDelimiter('{'); $smarty->setRightDelimiter('}'); //窗口提示信息 function alert($message,$url){ exit('<script>alert("'.$message.'");window.location.href="'.$url.'"</script>'); }
database.php:
<?php require __DIR__.'/../vendor/autoload.php'; use Medoo\Medoo as DB; //配置Medoo数据库信息 $config=[ 'database_type' => 'mysql', 'database_name' => 'edu', 'server' => 'localhost', 'username' => 'root', 'password' => 'root', ]; $db=new DB($config);