zoukankan      html  css  js  c++  java
  • 用Thinkphp实现表的添加修改和删除

    用mvc实现主要用到了c和v

    <?php
    namespace HomeController;
    use ThinkController;
     
    class MainController extends Controller
    {
        public function Main()
        {
            var_dump(get_defined_constants(true)); 
        }
        /*public function Info(){
            
            return "大苹果";
        }*/
        public function show()
        {
            /*$info=D("Info");
            var_dump($info);
             */
            $info=M("info");//创建对象
            //$info->table("info");
            $attr=$info->select();//连贯操作查询
            $this->assign("test","hello");
            $this->assign("info",$attr);
             $this->display();
             //var_dump($attr);
        }
        public function Add()
        {
            if(empty($_POST))
            {
                //打出添加页面
                $this->display();
            }
            else
            {
                //添加数据
                $nation=D("Info");//创建表
                //$code=$_POST["Code"];//传值
                //$name=$_POST["Name"];
                //数组方式
                //$attr=array("Code"=>$code,"Name"=>$name);
                //$nation->add($attr);
                //AR方式
                //$nation->Code=$code;
                //$nation->Name=$name;
                
                //$nation->add();
                //自动收集表单,数据库名称必须一致
                $nation->create();
                $z=$nation->add();
                if($z)
                {
                    $this->success("添加成功!","show");
                }
                else
                {
                    $this->error("添加失败!","Add");
                }
                
            }
        }
         public function Delete()
         {
             $code=$_GET["code"];
             $info=D("Info");
             $z=$info->delete($code);
             if($z)
             {
                 $this->success("删除成功",U("show"));
             }
             else
             {
                 $this->error("删除失败",U("show"));
             }
             }
         //数据修改
         public function Update()
         {
             $code= $_GET["code"];
             $info=D("Info");
             
              if(empty($_POST)) 
             {
                 
                 $attr=$info->find($code);
                 $this->assign("info",$attr);
             
                 $this->display();
             }
             else
             {
                 //修改数据
                /* $info->Code=$_POST['Code'];
                 $info->Name=$_POST["Name"];
                 $info->Sex=$_POST["Sex"];
                 $info->Nation=$_POST["Nation"];
                 $info->Birthday=$_POST["Birthday"];*/
                 //$code=$_POST["Code"];
                 $info->create(); 
                 $z=$info->save();
                 if($z)
                 {
                     $this->success("修改成功",U("show"));
                 }
                 else
                 {
                     $this->error("修改失败");
                  }
             }
         }
         public function test()
         {
             $nation=D("Nation");
             $sql="select * from Nation";
             $a=$nation->query($sql);
             var_dump($a);
             //如果是查询query(),如果是其他execute()
         }
         
         
    } 
    View Code
    <!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>
    </head>
    
    <body>
    <form action="__SELF__" method="post">
    <div>代号:<input type="text" name="Code" /></div>
    <div>姓名:<input type="text" name="Name"/></div>
    <div>性别:<input type="text" name="Sex"/></div>
    <div>民族:<input type="text" name="Nation"/></div>
    <div>生日:<input type="text" name="Birthday"/></div>
    <input type="submit" value="添加" />
    </form>
    </body>
    </html>
    <!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>
    </head>
    
    <body>
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
    <td>代号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
    <td>操作</td>
    </tr>
    
    <foreach name="info" item="v">
      <tr>
        <td>{$v.code}</td>
        <td>{$v.name}</td>
        <td>{$v.sex}</td>
        <td>{$v.nation}</td>
        <td>{$v.birthday}</td>
        <td><a href="__CONTROLLER__/Update/code/{$v.code}">修改</a>
        <a href="__CONTROLLER__/Delete/code/{$v.code}">删除</a>
        </td>
         
      </foreach>
     
      
      </table>
      
    <a href="__CONTROLLER__/Add/code/{$v.code}"><input type="submit" value="添加" /></a>
    
         
    </tr>
    </body>
    </html>
     <!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>
    </head>
    
    <body>
    <form action="__SELF__" method="post">
    <div><input type="hidden" name="Code"  value="{$info.code}"/></div>
    <div>姓名:<input type="text" name="Name"  value="{$info.name}"/></div>
    <div>性别:<input type="text" name="Sex" value="{$info.sex}" /></div>
    <div>民族:<input type="text" name="Nation" value="{$info.nation}"/></div>
    <div>生日:<input type="text" name="Birthday" value="{$info.birthday}" /></div>
    <input type="submit" value="修改" />
    </form>
    </body>
    </html>
    <?php
    namespace HomeController;
    use ThinkController;
     
    class MainController extends Controller
    {
        public function Main()
        {
            var_dump(get_defined_constants(true)); 
        }
        /*public function Info(){
            
            return "大苹果";
        }*/
        public function show()
        {
            /*$info=D("Info");
            var_dump($info);
             */
            $info=M("info");//创建对象
            //$info->table("info");
            $attr=$info->select();//连贯操作查询
            $this->assign("test","hello");
            $this->assign("info",$attr);
             $this->display();
             //var_dump($attr);
        }
        public function Add()
        {
            if(empty($_POST))
            {
                //打出添加页面
                $this->display();
            }
            else
            {
                //添加数据
                $nation=D("Info");//创建表
                //$code=$_POST["Code"];//传值
                //$name=$_POST["Name"];
                //数组方式
                //$attr=array("Code"=>$code,"Name"=>$name);
                //$nation->add($attr);
                //AR方式
                //$nation->Code=$code;
                //$nation->Name=$name;
                
                //$nation->add();
                //自动收集表单,数据库名称必须一致
                $nation->create();
                $z=$nation->add();
                if($z)
                {
                    $this->success("添加成功!","show");
                }
                else
                {
                    $this->error("添加失败!","Add");
                }
                
            }
        }
         public function Delete()
         {
             $code=$_GET["code"];
             $info=D("Info");
             $z=$info->delete($code);
             if($z)
             {
                 $this->success("删除成功",U("show"));
             }
             else
             {
                 $this->error("删除失败",U("show"));
             }
             }
         //数据修改
         public function Update()
         {
             $code= $_GET["code"];
             $info=D("Info");
             
              if(empty($_POST)) 
             {
                 
                 $attr=$info->find($code);
                 $this->assign("info",$attr);
             
                 $this->display();
             }
             else
             {
                 //修改数据
                /* $info->Code=$_POST['Code'];
                 $info->Name=$_POST["Name"];
                 $info->Sex=$_POST["Sex"];
                 $info->Nation=$_POST["Nation"];
                 $info->Birthday=$_POST["Birthday"];*/
                 //$code=$_POST["Code"];
                 $info->create(); 
                 $z=$info->save();
                 if($z)
                 {
                     $this->success("修改成功",U("show"));
                 }
                 else
                 {
                     $this->error("修改失败");
                  }
             }
         }
         public function test()
         {
             $nation=D("Nation");
             $sql="select * from Nation";
             $a=$nation->query($sql);
             var_dump($a);
             //如果是查询query(),如果是其他execute()
         }
         
         
    } 
  • 相关阅读:
    Hamming Distance
    变量
    Jupyter Notebook 快捷键使用指南
    XPath使用总结
    selenium调用webdriver异常
    漏洞挖掘 | 远程WWW服务支持TRACE请求
    漏洞挖掘 | 点击劫持
    漏洞挖掘 | 目录浏览漏洞
    漏洞挖掘 | 弱口令漏洞
    浅谈Linux下/etc/passwd文件
  • 原文地址:https://www.cnblogs.com/crazy-zw/p/5496907.html
Copyright © 2011-2022 走看看