• TP增删改


    <?php
    namespace HomeController;
    use ThinkController;
    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 HomeController;
    use ThinkController;
    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 HomeController;
    use ThinkController;
    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 HomeController;
    use ThinkController;
    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);
    			
    		}
    }
    

      

  • 相关阅读:
    浅谈异或运算^的作用
    牛客网剑指offer第40题——数组中只出现一次的数字(浅谈位运算的妙用)
    归并排序——一文吃透归并和递归的思想和完整过程!(没看懂请留言)
    深入分析二分查找及其变体
    vector构造函数的学习
    牛客网剑指offer第34题——找到第一个只出现一次的字符
    牛客网剑指offer第27题——求字符串的全排列
    牛客网剑指offer第20题——定义栈的数据结构
    C++重载运算简介
    Leetcode中字符串总结
  • 原文地址:https://www.cnblogs.com/xiaohaihuaihuai/p/8594778.html
Copyright © 2020-2023  润新知