填加数据:insert.php
<?php
/**
* 添加数据
*/
//引入smarty配置
include __DIR__.'/config/config.php';
//连接数据库
require __DIR__.'/medoo/conn.php';
@$act = $_REQUEST['act'];
if($act=='get') {
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
$sex = $_REQUEST['sex'];
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
$zt = $_REQUEST['status'];
$time = $_REQUEST['create_time'];
//插入数据操作
$table = 'user';
$data['name'] = $name;
$data['age'] = $age;
$data['sex'] = $sex;
$data['email'] = $email;
$data['password'] = sha1("$password");
$data['status'] = $zt;
$data['create_time'] = $time;
//查询用户名是否存在
$res = $db->select($table,'name');//查询数据库所有用户名
$isin = in_array($name,$res); //在查询结果中查找用户名是否存在
if ($isin){
echo "<script>alert('您添加的用户名已经存在!');location.href='insert.php'</script>";
exit();
}
//执行插入操作
$stmt = $db->insert($table, $data);
//查询受影响的记录数量
$num = $stmt->rowCount();
if ($num > 0) {
echo "<script>alert('成功添加了{$num}条记录');location.href='insert.php'</script>";
}
}
//模板渲染
$smarty->display('insert.html');
添加数据模板:insert.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加数据</title>
</head>
<body>
{include file="head.html"}
<form action="insert.php?act=get" method="post">
<input type="hidden" name="create_time" value="{$smarty.now}">
<table border="1" cellpadding="5" cellspacing="0" width="60%">
<caption>
<h2>添加用户</h2>
</caption>
<tr>
<td width="100"> 用户名:</td>
<td><input name="name" value="" type="text"></td>
</tr>
<tr>
<td> 密码:</td>
<td><input name="password" value="" type="password"></td>
</tr>
<tr>
<td> 年龄:</td>
<td><input name="age" value="" type="text"></td>
</tr>
<tr>
<td> 性别:</td>
<td><input name="sex" type="radio" value="0" checked />
男
<input name="sex" type="radio" value="1" />
女</td>
</tr>
<tr>
<td> 电子邮箱:</td>
<td><input name="email" type="text" value=""></td>
</tr>
<tr>
<td> 用户状态:</td>
<td><input name="status" type="radio" value="1" checked />
启用
<input name="status" type="radio" value="0" />
禁用</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
查询数据:search.php
<?php
/**
* 查询数据
*/
//引入smarty配置
include __DIR__.'/config/config.php';
//连接数据库
require __DIR__.'/medoo/conn.php';
$rows =[];
@$act = $_REQUEST['act'];
if($act=='so') {
//设置表名 要查询的数据
$table = 'user';
$data = ['user_id','name','sex','age','email','create_time'];
//接收表单传过来的数据
$sel = $_REQUEST['sel'];
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
@$sex = $_REQUEST['sex'];
//性别不为空
if ($sex !='') {
//性别不为空 姓名不为空
if ($name != '') {
//性别不为空 姓名不为空 年龄不为空
if ($age != '') {
if ($sel == '='){
$where = ['AND' => ['name' => $name, 'age' => $age, 'sex' => $sex]];
}elseif ($sel == '>'){
$where = ['AND' => ['name' => $name, 'age[>]' => $age, 'sex' => $sex]];
}else{
$where = ['AND' => ['name' => $name, 'age[<]' => $age, 'sex' => $sex]];
}
//性别不为空 姓名不为空 年龄为空
}else {
$where = ['AND' => ['name' => $name, 'sex' => $sex]];
}
//性别不为空 姓名为空
} else {
//性别不为空 姓名为空 年龄不为空
if ($age != '') {
if ($sel == '=') {
$where = ['AND' => ['age' => $age,'sex'=>$sex]];
} elseif ($sel == '>') {
$where = ['AND' => ['age[>]' => $age,'sex'=>$sex]];
} else {
$where = ['AND' => ['age[<]' => $age,'sex'=>$sex]];
}
//性别不为空 姓名为空 年龄为空
} else {
$where = ['sex'=>$sex];
}
}
//性别为空
}else {
//性别为空 姓名不为空
if ($name != '') {
//性别为空 姓名不为空 年龄不为空
if ($age != '') {
if ($sel == '=') {
$where = ['AND' => ['age' => $age,'name'=>$name]];
} elseif ($sel == '>') {
$where = ['AND' => ['age[>]' => $age,'name'=>$name]];
} else {
$where = ['AND' => ['age[<]' => $age,'name'=>$name]];
}
//性别为空 姓名不为空 年龄为空
} else {
$where = ['name'=>$name];
}
//性别为空 姓名为空
}else{
//性别为空 姓名为空 年龄不为空
if ($age != '') {
if ($sel == '=') {
$where = ['age' => $age];
} elseif ($sel == '>') {
$where = ['age[>]' => $age];
} else {
$where = ['age[<]' => $age];
}
//性别为空 姓名为空 年龄为空
} else {
$where = '';
}
}
}
// 执行查询操作
$res = $db->select($table,$data,$where);
//遍历结果集
foreach ($res as $row){
$rows[] = $row;
}
//进行模板变量替换
$smarty->assign('rows',$rows);
}
$smarty->assign('act',$act);
//模板渲染
$smarty->display('search.html');
查询数据模板:search.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询数据</title>
</head>
<body>
{include file="head.html"}
<form action="search.php?act=so" method="post">
<table border="1" cellpadding="5" cellspacing="0" align="center" width="60%">
<caption>
<h2>用户查询</h2>
</caption>
<tr>
<td width="100">用户名:</td>
<td><input type="text" name="name" value="">
</td>
</tr>
<tr>
<td>性别:</td>
<td><input name="sex" type="radio" value="0" />男
<input name="sex" type="radio" value="1" />女
</td>
</tr>
<tr>
<td>年龄:</td>
<td><select name="sel">
<option value="=">=</option>
<option value=">">></option>
<option value="<"><</option>
</select>
<input name="age" type="text" value="" />
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"> <input type="reset" value="清除所有条件">
</td>
</tr>
</table>
</form>
<br><br>
{if $act eq 'so'}
<table border="1" cellpadding="5" cellspacing="0" align="center" width="60%">
<caption><h2>用户信息表</h2></caption>
<tr bgcolor="#90ee90">
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>邮箱</th>
<th>入职时间</th>
</tr>
{foreach $rows as $row}
<tr align="center">
<td>{$row.user_id}</td>
<td>{$row.name}</td>
<td>{if $row.sex eq '0'}
男
{else}
女
{/if}
</td>
<td>{$row.age}</td>
<td>{$row.email}</td>
<td>{$row.create_time|date_format:"%Y/%m/%d %H:%M:%S "}</td>
</tr>
{foreachelse}
<tr align="center"><td colspan="6"><h3>没有符合条件的数据</h3></td></tr>
{/foreach}
</table>
{/if}
</body>
</html>
更改数据:update.php
<?php
/**
* 更新操作
*/
//引入smarty配置
include __DIR__.'/config/config.php';
//连接数据库
require __DIR__.'/medoo/conn.php';
@$act=$_REQUEST['act'];
if ($act=='update'){
//接收表单提交的值
$sel = $_REQUEST['sel'];
$name = $_REQUEST['name'];
@$sex = $_REQUEST['sex'];
$age = $_REQUEST['age'];
$email= $_REQUEST['email'];
//要更新的数据
$table='user';
//判断用户名是否为空
if ($name ==''){
echo "<script>alert('请填写用户名!');location.href='update.php'</script>";
exit();
//判断用户名是否在数据库有对应记录
}else{
$res = $db->select($table,'name');//查询数据库所有用户名
$isin = in_array($name,$res); //在查询结果中查找用户名是否存在
if (!$isin){
echo "<script>alert('您要更新的用户名不存在!请重试!');location.href='update.php'</script>";
exit();
}
}
//性别不为空
if ($sex !=''){
//性别不为空 年龄不为空
if ($age !=''){
//性别不为空 年龄不为空 邮箱不为空
if ($email !=''){
if ($sel =='+'){
$data['email'] = $email;
$data['age[+]'] = $age;
$data['sex'] = $sex;
}else{
$data['email'] = $email;
$data['age'] = $age;
$data['sex'] = $sex;
}
//性别不为空 年龄不为空 邮箱为空
}else{
if ($sel =='+'){
$data['age[+]'] = $age;
$data['sex'] = $sex;
}else{
$data['age'] = $age;
$data['sex'] = $sex;
}
}
//性别不为空 年龄为空
}else{
//性别不为空 年龄为空 邮箱不为空
if ($email !=''){
$data['email'] = $email;
$data['sex'] = $sex;
//性别不为空 年龄为空 邮箱为空
}else{
$data['sex'] = $sex;
}
}
//性别为空
}else{
//性别为空 年龄不为空
if ($age !=''){
//性别为空 年龄不为空 邮箱不为空
if ($email !=''){
if ($sel =='+'){
$data['email'] = $email;
$data['age[+]'] = $age;
}else{
$data['email'] = $email;
$data['age'] = $age;
}
//性别为空 年龄不为空 邮箱为空
}else{
if ($sel =='+'){
$data['age[+]'] = $age;
}else{
$data['age'] = $age;
}
}
//性别为空 年龄为空
}else{
//性别为空 年龄为空 邮箱不为空
if ($email !=''){
$data['email'] = $email;
//性别为空 年龄为空 邮箱为空
}else{
echo "<script>alert('请填写更新信息!');location.href='update.php'</script>";
exit();
}
}
}
//更新条件
$where['name'] =$name;
//执行更新操作
$stmt = $db->update($table,$data,$where);
$num = $stmt->rowCount();
if ($num>0){
echo "<script>alert('成功更新了{$num}条记录');location.href='update.php'</script>";
}
}
//模板渲染
$smarty->display('update.html');
更改数据模板:update.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>更新数据</title>
</head>
<body>
{include file="head.html"}
<form action="update.php?act=update" method="post">
<table border="1" cellpadding="5" cellspacing="0" align="center" width="60%">
<caption>
<h2>用户信息修改</h2>
</caption>
<tr>
<td width="100">用户名:<br>(条件,必填)</td>
<td><input type="text" name="name" value="">
</td>
</tr>
<tr>
<td>性别:</td>
<td><input name="sex" type="radio" value="0" />男
<input name="sex" type="radio" value="1" />女
</td>
</tr>
<tr>
<td>年龄:</td>
<td><select name="sel">
<option value="">=</option>
<option value="+">+</option>
</select>
<input name="age" type="text" value="" />
</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input name="email" type="text" value="" />
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"> <input type="reset" value="清除所有条件">
</td>
</tr>
</table>
</form>
</body>
</html>
删除数据:delete.php
<?php
/**
* 删除数据
*/
//引入smarty配置
include __DIR__.'/config/config.php';
//连接数据库
require __DIR__.'/medoo/conn.php';
@$act=$_REQUEST['act'];
if ($act=='del') {
//接收表单传过来的数据
$id = $_REQUEST['id'];
$aqm = $_REQUEST['aqm'];
//验证安全码
if ($aqm !='php'){
echo "<script>alert(' 安全码不正确!请重试!');location.href='delete.php'</script>";
exit();
}
//设置表名
$table = 'user';
$where['user_id'] = $id;
//判断id是否为空
if ($id ==''){
echo "<script>alert('请填写用户ID!');location.href='delete.php'</script>";
exit();
//判断id是否在数据库有对应记录
}else{
$res = $db->select($table,'user_id');//查询数据库所有用户ID
$isin = in_array($id,$res); //在查询结果中查找用户ID是否存在
if (!$isin){
echo "<script>alert('您要删除的用户ID不存在!请重试!');location.href='delete.php'</script>";
exit();
}
}
//执行删除操作
$stmt= $db->delete($table,$where);
$num=$stmt->rowCount();
if ($num>0){
echo "<script>alert('成功删除了{$num}条记录');location.href='delete.php'</script>";
}
}
$smarty->display("delete.html");
删除数据模板:delete.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>删除用户</title>
</head>
<body>
{include file="head.html"}
<form action="delete.php?act=del" method="post">
<table border="1" cellpadding="5" cellspacing="0" align="center" width="60%">
<caption>
<h2>用户删除</h2>
</caption>
<tr>
<td width="100">用户id:<br>(条件,必填)</td>
<td><input type="text" name="id" value="">
</td>
</tr>
<tr>
<td width="100">安全码:</td>
<td><input type="text" name="aqm" value="">(为避免误操作,安全码为必填,安全码默认为:php)
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
里面用了大量的if else嵌套,是否有更简单高效的处理函数,请老师指点,谢谢,
演示地址:http://demo.lidiy.cn