ThinkPHP的CURD操作

一 代码

1、入口文件
<?php
define('THINK_PATH', '../ThinkPHP');		//定义ThinkPHP框架路径(相对于入口文件)
define('APP_NAME', 'App');				//定义项目名称
define('APP_PATH', './App');				//定义项目路径
require(THINK_PATH."/ThinkPHP.php");	//加载框架入口文件 
App::run();								//实例化一个网站应用实例
?>
 
2、配置文件
<?php 
return array(
	'DB_TYPE'=> 'pdo', 
	// 注意DSN的配置针对不同的数据库有所区别
	'DB_DSN'=> 'mysql:host=localhost;dbname=db_database30',
	'DB_USER'=>'root', 
	'DB_PWD'=>'root', 
	'DB_PREFIX'=>'think_',
	// 其他项目配置参数………
	'APP_DEBUG' => true, 		// 关闭调试模式
	'SHOW_PAGE_TRACE'=>true,
);
?>
 
3、控制器文件
<?php
header("Content-Type:text/html; charset=utf-8");	//设置页面编码格式
class IndexAction extends Action{
	public function index(){
		$db = M('User');							// 实例化模型类,参数数据表名称,不包含前缀
		$select = $db->where('user="mr"')->order('id desc')->limit(3)->select();
		$this->assign('select',$select); 			// 模板变量赋值
		$this->display(); 							// 指定模板页
	}
	public function insert(){
		$dba = M('User');							// 实例化模型类,参数数据表名称,不包含前缀
		$data['user'] = 'mr';
		$data['pass'] = md5('mrsoft');
		$data['address'] = '长春市';
		$result=$dba->add($data);
		if($result){
			$this->redirect('Index/index','', 2,'页面跳转中');		//页面重定向	
		}
	}
}
?>
 
4、模板文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户信息输出</title>
<link href="__ROOT__/Public/Css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
  <tr>
    <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>
  </tr>
  <tr class="title">
    <td bgcolor="#FFFFFF" width="44">ID</td>
    <td bgcolor="#FFFFFF" width="120">名称</td>
    <td bgcolor="#FFFFFF" width="223">地址</td>
  </tr>
  <foreach name='select' item='user' >
  <tr class="content">
    <td bgcolor="#FFFFFF">&nbsp;{$user.id}</td>
    <td bgcolor="#FFFFFF">&nbsp;{$user.user}</td>
    <td bgcolor="#FFFFFF">&nbsp;{$user.address}</td>
  </tr>
  </foreach>
</table>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
  <tr>
    <td colspan="3" bgcolor="#FFFFFF" class="title" align="center"><form id="form1" name="form1" method="post" action="__URL__/insert">
        <input type="submit" name="button" id="button" value="数据添加" />
    </form></td>
  </tr>
</table>
</body>
</html>
 
二 运行结果

 

猜你喜欢

转载自cakin24.iteye.com/blog/2381199