CURD演示 2

时间:2022-08-03 15:42:37
<?php
class UserAction extends Action{
public function index(){
echo "你好!";
$m=M('user');
$arr=$m->select();
#var_dump($arr);
$this->assign('data',$arr);
$this->display(); } public function del(){
$m=M('user');
##删除具体id数据
$id=$_GET['id'];
$count=$m->delete($id);
echo $count;
if ($count>0){
$this->success('数据删除成功');
}else{
$this->error('数据删除失败');
}
}
//负责修改页面
public function modify(){
$id=$_GET['id'];
$m=M('user');
$arr=$m->find($id);
$this->assign('data',$arr);
$this->display();
} public function update(){ $m=M('user');
$data['id']=$_POST['id'];
$data['username']=$_POST['username'];
$data['sex']=$_POST['sex'];
$count=$m->save($data);
if ($count>0){
$this->display('User/index');
}else{
$this->error('数据修改失败');
} }
} ?> 这里的 $this->display('User/index'); 会去找模板不存在[./Home/Tpl/User/User/index.html] 需要改为 $this->display('index'); public function update(){ $m=M('user');
$data['id']=$_POST['id'];
$data['username']=$_POST['username'];
$data['sex']=$_POST['sex'];
$count=$m->save($data);
if ($count>0){
$this->success('数据修改成功','index');
}else{
$this->error('数据修改失败'); 数据修改成功后,返回User/index页面 button 按钮: <button>添加用户</button> <form action="/thinkphp/index.php/User/create" method='post'>' 等价于 <form action="__URL__/create" method='post'> /*** <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script>
function jump(){
window.location="http://localhost:8080/thinkphp/index.php/User/add";
}
</script>
</head>
<body>
<h1>scan show 你好 hhhello world</h1>
<table border='1' width='500' align='center'>
<tr>
<th>id</th>
<th>username</th>
<th>sex</th>
<th>操作</th>
</tr> <volist name='data' id='vo'>
<tr>
<td><{$vo.id}></td>
<td><{$vo.username}></td>
<td><{$vo.sex}></td>
<td><a href="http://localhost:8080/thinkphp/index.php/User/del/id/<{$vo.id}>">删除</a>|<a href="http://localhost:8080/thinkphp/index.php/User/modify/id/<{$vo.id}>">修改</a></td>
</tr>
</volist>
</table>
<center>
<button onclick="jump()">添加用户</button>
</center>
</body>
</html> 添加用户 调用 window.location="http://localhost:8080/thinkphp/index.php/User/add"; add.html: <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title> </head>
<body>
<form action="__URL__/create" method='post'>
id:<input type='text' name='id' /></br>
姓名:<input type="text" name='username' /></br>
性别:男<input type='radio' name='sex' value='1' >
女<input type='radio' name='sex' value='0'></br>
<input type="submit" value='添加用户'/></br>
</form>
</body>
</html>