您的位置:首页 > 编程语言 > PHP开发

ThinkPHP学习笔记(六)使用CURD对User数据库数据进行删除和修改

2013-08-05 23:32 561 查看
ThinkPHP学习笔记(一)环境搭建

ThinkPHP学习笔记(二)MVC模式和URL访问

ThinkPHP学习笔记(三)输出

ThinkPHP学习笔记(四)模型与数据库

ThinkPHP学习笔记(五)使用模型操作数据库之CURD
ThinkPHP学习笔记(六)使用CURD对User数据库数据进行删除和修改

ThinkPHP学习笔记(七)深入学习数据库查询

一、建立项目与配置项目及数据库配置等内容:请参考《ThinkPHP学习笔记(一 ~ 四)》,本文假设已经创建好了项目及数据库。

二、基本的CURD操作:《ThinkPHP学习笔记(五)使用模型操作数据库之CURD

三、User数据操作:

如图:实现对用户数据展示、删除、修改



代码:

1、在C层中增加UserAction.class.php

然后访问:http://localhost/ThPHP/index.php/User/index

<?php
class UserAction extends Action {
public function index() {
// $m=new Model('User');
//  		$m->username='lizhongfu';
// 			$m->sex='1';
//  		$m->add(); //有返回值

$m=M('User');
$arr=$m->select();
// var_dump($arr);
$this->assign('data', $arr);
$this->display();
}
public function del() {
$m=M('User');
$id=$_GET['id'];
$count=$m->delete($id);
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->success('数据修改成功','index');
} else {
$this->error('数据修改失败');
}
}
public function add() {
$this->display();
}
public function create() {
$m=M('User');
$data['username']=$_POST['username'];
$data['sex']=$_POST['sex'];
$idNum=$m->add($data);
if($idNum>0) {
$this->success('数据添加成功','index');
} else {
$this->error('数据添加失败');
}
}
}
?>


2、V层添加展示User表内容的index.html 和 修改数据页面 modify.html(点击修改按钮跳转到的页面)

index.html

<html>

<head>
<title>Test</title>
<script>
function jump() {
window.location="__URL__/add";
}
</script>
</head>

<body>
<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="/ThPHP/index.php/User/del/id/<{$vo.id}>">删除</a> | <a href="/ThPHP/index.php/User/modify/id/<{$vo.id}>">修改</a></td>
</tr>
</volist>
</table>
<center>
<button onclick="jump()">页面跳转</button>
</center>
</body>

</html>


modify.html:

<html>
<head>
<title>modify</title>
<script>
window.onload=function () {
if(<{$data.sex}>==0) {
document.getElementsByName('sex')[1].checked='checked';
} else {
document.getElementsByName('sex')[0].checked='checked';
}
}
</script>
</head>

<body>
<form action="/ThPHP/index.php/User/update" method='POST'>
<input type='hidden' name='id' value="<{$data.id}>">
姓名:<input type='text' name='username' value="<{$data.username}>"/><br/>
性别:男<input type='radio' name='sex' value='1'/>女<input type='radio' name='sex' value='0'/><br/>
<input type='submit' value='提交修改'/>
</form>
</body>

</html>

add.html

<html>
<head>
<title>add</title>
</head>

<body>
<form action="__URL__/create" method='POST'>
姓名:<input type='text' name='username' value="<{$data.username}>"/><br/>
性别:男<input type='radio' name='sex' value='1'/>
女<input type='radio' name='sex' value='0'/><br/>
<input type='submit' value='添加新用户'/>
</form>
</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐