要操作的数据库如下:
数据库表名为admin
详细讲解都写在代码注释里,如下
控制器类IndexController.class.php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){//默认打开方法,执行了一个查询操作
$id=$_GET['id'];//
$model=D("admin");//实例化AdminMOdel.class.php模型类
$res=$model->cha();//执行查询操作返回一个二维数组
$this->assign('hema',$res);//分配变量
$this->display('intest');//渲染视图
}
public function shan(){//删除方法
$re='muban';
$id=$_GET['id'];
$model=D("admin");
$res=$model->shanchu($id);//执行删除操作
header("location:http://wgh.localhost.com/lianxi/tp/index.php/Home/index/index");//返回默认首页
}
public function xiugai(){//修改方法
if(isset($_POST)&& !empty($_POST)){//判断$_POST是否存在、是否为空,间接判断用户是否对要修改的数据执行了提交操作
$arr=$_POST;//将用户提交的数组数据赋值给变量$arr
$id=$_GET['id'];//要修改的用户数据的id
$model=D("admin");//实例化AdminMOdel模型类
$res=$model->gai($id,$arr);//对指定id的用户数据执行修改操作,$arr为用户提交的修改数据
header("location:http://wgh.localhost.com/lianxi/tp/index.php/Home/index/index");
}else{//进入修改页面用户未提交修改操作时执行的操作
$id=$_GET['id'];
$model=D("admin");
$res=$model->chaxun($id);//获取指定id的用户数据,返回一个一维数组
$this->assign('vo',$res);//分配变量
$this->display('xiugai');//渲染视图
}
}
public function tianjia(){//添加方法
if(isset($_POST)&& !empty($_POST)){//通过判断$_POST是否存在,间接判断用户是否已执行添加提交操作
$arr=$_POST;
$model=D("admin");//实例化模型类
$res=$model->jia($arr);//调用模型类中的jia方法完成添加操作
header("location:http://wgh.localhost.com/lianxi/tp/index.php/Home/index/index");
}
$this->display('tianjia');//渲染视图,用户刚进入添加页面时执行的操作
}
模型类AdminModel.class.php
namespace Home\Model;
use Think\Model;
class AdminModel extends Model {
public function cha(){//查询数据表,返回一个二维数组
// return 999;
return $this->select();
}
public function chaxun($id){//根据id查询某个用户的数据,返回一个一维数组
// return 999;
return $this->where("id=$id")->find();
}
public function jia($arr){//将数组形式的数据插入数据库
return $this->add($arr);
}
public function gai($id,$arr){//根据id对指定用户的数据执行修改操作
return $this->where("id=$id")->save($arr);
}
public function shanchu($id){//根据指定id对某条数据执行删除操作
return $this->where("id=$id")->delete();
}
}
视图文件intest.html,xiugai.html与tianjia.html
intest.html
<!DOCTYPE html>
<html>
<head>
<title>intest模板</title>
</head>
<body>
<p>这是index的模板</p>
<p>{$hema}</p>
<a href="http://wgh.localhost.com/lianxi/tp/index.php/Home/index/tianjia">添加用户</a>
<br/>
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>密码</td>
<td>删除</td>
<td>修改</td>
</tr>
<!-- tp框架内置标签遍历二维数组 -->
<foreach name="hema" item="v">
<!-- {$v.name} -->
<tr>
<td>{$v.name}</td>
<td>{$v.age}</td>
<td>{$v.password}</td>
<td><a href="http://wgh.localhost.com/lianxi/tp/index.php/Home/index/shan?id={$v['id']}">删除</a></td>
<td><a href="http://wgh.localhost.com/lianxi/tp/index.php/Home/index/xiugai?id={$v['id']}">修改</a></td>
</tr>
<!--
<br /> -->
</foreach>
<br />
</table>
<!-- <p>{$hema|var_dump}</p> -->
</body>
</html>
xiugai.html
<!DOCTYPE html>
<html>
<head>
<title>intest模板</title>
</head>
<body>
<p>这是index的模板</p>
<p>{$vo|var_dump}</p>
<form action="http://wgh.localhost.com/lianxi/tp/index.php/Home/index/xiugai?id={$vo['id']}"method="post">
姓名:<input type="text" name="name"value="{$vo.name}"><br/>
年龄:<input type="text" name="age"value="{$vo.age}"><br/>
密码:<input type="text" name="password"value="{$vo.password}"><br/>
<input type="submit" name="submit"value="确认修改">
</form>
<!-- <foreach name="hema" item="v">
{$v.name}
<a href="http://wgh.localhost.com/lianxi/tp/index.php/Home/index/shan?id={$v['id']}">删除账户为:{$v['name']}</a>
<br />
</foreach>
<br />
<p>{$hema|var_dump}</p> -->
</body>
</html>
tianjia.html
<!DOCTYPE html>
<html>
<head>
<title>intest模板</title>
</head>
<body>
<p>这是index的模板</p>
<p>{$vo|var_dump}</p>
<form action="http://wgh.localhost.com/lianxi/tp/index.php/Home/index/tianjia"method="post">
姓名:<input type="text" name="name"value=""><br/>
年龄:<input type="text" name="age"value=""><br/>
密码:<input type="text" name="password"value=""><br/>
<input type="submit" name="submit"value="确认添加">
</form>
<!-- <foreach name="hema" item="v">
{$v.name}
<a href="http://wgh.localhost.com/lianxi/tp/index.php/Home/index/shan?id={$v['id']}">删除账户为:{$v['name']}</a>
<br />
</foreach>
<br />
<p>{$hema|var_dump}</p> -->
</body>
</html>