TP增删改

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    $db = D("Nation");
	$arr = $db->select(); 
	$this->assign("d","145223121"); 
	$this->assign("arr",$arr);
	$this->show(); 
    }
	
	public  function add(){
		
		$db = D("Info");
		//使用数组方式
		//$arr = array("Code"=>"p009","Name"=>"李四","Sex"=>1,
		//"Nation"=>"n002","Birthday"=>"1988-2-3");
		//$db->add($arr);
		
		//使用映射的方式
		//$db->Code = "p010";
		//$db->Name = "王五";
		//$db->Sex = 1;
		//$db->Nation = "n001";
		//$db->Birthday = "1990-3-4";
		//$db->add();
		
	}
}

 

自动收集表单的方法

首先在index文件夹中新建一个add文件

<!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>
<style type="text/css">	
		
</style>
</head> 
 
<body> 

<form action="__ACTION__" method="post">
	<div>代号:<input type="text" name="Code" /></div>
	<div>姓名:<input type="text" name="Name" /></div>
	<div>性别:<input type="radio" name="Sex" value="1" />男
			  <input type="radio" name="Sex"  value="0" />女
	</div>
	  <div>民族:
        <select name="Nation">
            <foreach name="arr" item="v">
                <option value="{$v.code}">{$v.name}</option>
            </foreach>
        </select>
    </div>
	<div>生日:<input type="text" name="Birthday" /></div>
	<input type="submit" value="添加" />
</form>

 
</body>
   
</html>
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    $db = D("Nation");
	$arr = $db->select(); 
	$this->assign("d","145223121"); 
	$this->assign("arr",$arr);
	$this->show(); 
    }
	
	public  function add(){
		
		$db = D("Info");
		//使用数组方式
		//$arr = array("Code"=>"p009","Name"=>"李四","Sex"=>1,
		//"Nation"=>"n002","Birthday"=>"1988-2-3");
		//$db->add($arr);
		
		//使用映射的方式
		//$db->Code = "p010";
		//$db->Name = "王五";
		//$db->Sex = 1;
		//$db->Nation = "n001";
		//$db->Birthday = "1990-3-4";
		//$db->add();
		
		//自动收集表单
		if(empty($_POST)){
			$arr=$db
			->table("nation")
			->select();
			$this->assign("arr",$arr);
			$this->show();
		}else{
			//收集表单
			$db->create();
			$db->Sex = $_POST["Sex"]?true:false;
			$db->add();
		}
		
	}
}

 

修改数据

<!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>
<style type="text/css">	
		
</style>
</head> 
 
<body> 

<form action="__ACTION__" method="post">
	<div><input type="hidden" name="Code" value="{$info.code}" /></div>
	<div>姓名:<input type="text" name="Name" value="{$info.name}" /></div>
	<div>性别:
		<if condition="$info.sex=='1'">
			<input type="radio" name="Sex" value="1" checked="checked" />男
			 <input type="radio" name="Sex"  value="0" />女
			 <else/>
		
	          <input type="radio" name="Sex" value="1" />男
			  <input type="radio" name="Sex"  value="0" checked="checked" />女
		</if>
	</div>
	  <div>民族:
        <select name="Nation">
            <foreach name="nation" item="v">
			<if condition="$v.code==$info.nation">
			<option value="{$v.code}" selected="selected">{$v.name} </option>
			<else/>
                <option value="{$v.code}">{$v.name}</option>
				</if>
            </foreach>
        </select>
    </div>
	<div>生日:<input type="text" name="Birthday" value="{$info.birthday}" /></div>
	<input type="submit" value="修改" />
</form>

 
</body>
   
</html>

 

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){;
       $this->assign("d","134212534");;
    }
    public function add(){
        $db = D("Info");
        //3.自动收集表单
        if(empty($_POST)){
            $arr = $db
            ->table("Nation")
            ->select();
            $this->assign("arr",$arr);
            $this->show();
        }else{
             
            $db->create();//收集表单
            $db->add();
        }
    }
    public function update(){
         
        $db = D("Info");
        $code = "p002";
        if(empty($_POST)){
             
            $nation = $db
            ->table("Nation")
            ->select();
             
            $info = $db
            ->find($code);
             
            $this->assign("info",$info);
            $this->assign("nation",$nation);
            $this->show();
        }else{
            $db->create();
            $db->save();
        }
    }
}

删除数据

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    $db = D("Nation");
	$arr = $db->select(); 
	$this->assign("d","145223121"); 
	$this->assign("arr",$arr);
	$this->show(); 
    }
	
	public  function add(){
		
		$db = D("Info");
		//使用数组方式
		//$arr = array("Code"=>"p009","Name"=>"李四","Sex"=>1,
		//"Nation"=>"n002","Birthday"=>"1988-2-3");
		//$db->add($arr);
		
		//使用映射的方式
		//$db->Code = "p010";
		//$db->Name = "王五";
		//$db->Sex = 1;
		//$db->Nation = "n001";
		//$db->Birthday = "1990-3-4";
		//$db->add();
		
		//自动收集表单
		if(empty($_POST)){
			$arr=$db
			->table("nation")
			->select();
			$this->assign("arr",$arr);
			$this->show();
		}else{
			//收集表单
			$db->create();
			$db->Sex = $_POST["Sex"]?true:false;
			$db->add();
		}
		
	}
		public function update(){
			$code = "p010";
			$db = D("Info");
			if(empty($_POST)){
				
				$nation = $db
			    ->table("nation")
			    ->select();
				
				$info = $db
			    ->find($code);
				
				$this->assign("info",$info);
				$this->assign("nation",$nation);
				$this->show();
			}else{
				$db->create();
				$db->save();
			}
			
		}
		public function del(){
			
			$code = "p009";
			$db = D("Info");
			$db ->delete($code);
			
		}
}

  

posted @ 2018-03-18 11:29  小孩坏坏  阅读(139)  评论(0编辑  收藏  举报