ThinkPHP CURD(增删改查练习)

ThinkPHP     CURD 增删改查练习代码记录

 

控制器代码:

<?php

class IndexAction extends Action {
    public function index(){
       $m = M('User');
	   $arr = $m->select();
		$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['uid'] = $_POST['id'];
		$data['username'] = $_POST['username'];
		$data['email'] = $_POST['email'];
		$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");
		//dump($m);exit;
		/* $data['username'] = $_POST['username'];
		$data['sex'] = $_POST['sex']; */
		$m->username = $_POST['username'];
		$m->email = $_POST['email'];
		$m->sex = $_POST['sex'];
		$idNum = $m->add($data);
		if($idNum>0){
			$this->success('数据add成功','index');
		}else{
			$this->error('add失败');
		}

	}
}

控制器Index对应的模板文件(Index/index.html):

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<script>
			function jump(){
				window.location='/index.php/Index/add';
			}

		</script>
	</head>
<body>
	<table border='1' width='400' align='center'>
		<tr>
			<th>Uid</th>
			<th>Username</th>
			<th>sex</th>
			<th>email</th>
			<th>操作</th>
		</tr>

		<volist name='data' id='vo'>
		<tr>
			<td>{$vo.uid}</td>
			<td>{$vo.username}</td>
			<td>{$vo.sex}</td>
			<td>{$vo.email}</td>
			<td><a href='__URL__/del/id/{$vo.uid}'>删除 </a>| <a href='__URL__/modify/id/{$vo.uid}'>修改</a></td>
		</tr>
		</volist>
		<button onclick='jump()'><center>添加用户</center></button>
	</table>
</body>
</html>

控制器modify对应的模板文件(Index/modify.html):

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<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='__URL__/update'  method='post'>

		<input type='hidden' name='id' value='{$data.uid}'>
		姓名:<input type='text' name='username' value='{$data.username}' /> <br>
		Emial:<input type='text' name='email' value='{$data.email}' /> <br>
		性别:男<input type='radio' name='sex' value='1' />女<input type='radio' name='sex' value='0' /> <br>
		<input type='submit' value='修改'>

	</form>
</body>
</html>

控制器add对应的模板文件(Index/add.html):


<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	</head>
<body>
	<form action='__URL__/create'  method='post'>
		姓名:<input type='text' name='username' /> <br>
		Emial:<input type='text' name='email'/> <br>
		性别:男<input type='radio' name='sex' value='1' />女<input type='radio' name='sex' value='0' /> <br>
		<input type='submit' value='增加新用户'>

	</form>
</body>
</html>

数据库表:


--
-- 表的结构 `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `uid` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `sex` int(10) DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB  DEFAULT CHARSET=ucs2 AUTO_INCREMENT=10 ;

Jason.wang

When you find your talent can't afford to be ambitious, be quiet and study !

You may also like...