码迷,mamicode.com
首页 > 数据库 > 详细

数据库访问CRUD;__SELF__和__ACTION__的区别;自动收集表单:$n->create();

时间:2016-12-22 22:56:43      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:distinct   page   指定   namespace   使用   har   eth   png   自身   

 一、tp框架数据访问(pdo基础)

技术分享
public function test()
{
  $n = D("Nation");

 //select();find();    //查询

  1.$attr = $n->select();//查所有,返回一个关联数组,二维关联数组
  2.$attr = $n->select("n001,n002");//根据参数(主键值)查
  3.$attr = $n->find("n002");//查一条数据,一维数组
 //where    //连贯操作
  4.$n->where()->select();//加条件,$n->where()返回一个对象 
    $n->where("name=>‘汉族‘ or name=‘回族‘")->select();
  5.table    //如果查Nation表以外的数据,用table
    $attr = $n->table("info")->select();
  6.field    //指定字段查询(查某几列)
    $attr = $n->field("code")->select();
  7.order    //排序
    $attr = $n->order("code desc")->select();
  8.limit    //分页
    $a = $n->limit(3,5)->select();//跳过3条数据取5条数据
    $a = $n->limit(3)->select();//取3条数据
  9.page    //分页
    $a = $n->page(3,5)->select();//第3页显示5条数据,根据条数来自动分页
  10.group    //分组
    $a = $nation->field("Brand,avg(Price)")->group("Brand")->select();
  11.having    //分完组加条件
    $a = $nation->field("Brand,avg(Price)")->group("Brand")->having("avg(Price)>40")->select();
  12.distinct    //去重
    $a = $nation->field("Brand")->distinct(true)->select();
  13.getField    //获取某字段的数据,只能给列名查询,不写where条件,默认给索引最小的字段
    $a = $nation->where("code=‘n001‘")->getField("name");
  14.sum,count,max,min 
    $a = $nation->table("car")->sum(Price);
  15.join..on    //给索要查询的列都 as 给一个别名显示,别名自定义即可    
    $a = $nation->field("Info.code as ‘code‘,Info.name as ‘name‘,Nation.name as ‘民族‘")->join("Info on Info.nation=Nation.code")->select();
  16.cache    //数据缓存
  17.query    //原生sql语句查询
    $sql = "select * from nation";
    $a = $nation->query($sql);
  18.execute    //原生sql语句其他操作 修改    
    $sql = "update Nation set name=‘满族‘ where code=‘n001‘";
    $a = $nation->execute($sql);

  var_dump($a);
}
View Code

二、自动收集表单:

Application/Lianxi/controller/MainController.class.php

技术分享
<?php
namespace Lianxi\Controller;
use Think\Controller;
class MainController extends Controller
{
    public function Biaodan()
    {
        //实现两个逻辑
        //1.显示添加页面2.往数据库添加数据
        if(empty($_POST))
        {
            $this->show();    
        }
        else
        {
            $n = D("Nation");
            $n->create();//自动收集表单,前提是必须有post数据,如果post为空,走if;html页面中input里面的name一定要对应数据库表的的列名,大小写敏感    
            $z = $n->add();
            if($z)
            {
                //跳转方法一
                //$this->success("添加成功","Biaodan",5);//5是跳转时间,间隔5秒
                //跳转方法二
                $this->redirect(‘Biaodan‘,array(),5,"页面跳转中...");//array(‘id‘=>2),跳转页面的同时可以传参数,关联    
            }
            else
            {
                $this->error("添加失败!");//一个参数就可以,error失败默认返回前一个页面    
            }
        }
    }    
    //通过地址栏传参数,如:Main/canshu/id/5,方法来接受使用
    public function canshu($id)
    {
        //方法一
        //echo $_GET["id"];    
        //方法二:canshu($id)
        echo $id;//如果给$id赋值canshu($id=0)时,地址栏不给参数也不会报错
    }
}
View Code

Application/Lianxi/view/Main/Biaodan.html

技术分享
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="__SELF__" method="post">
<div>代号:<input type="text" name="Code" /></div>
<div>名称:<input type="text" name="Name" /></div>
<input type="submit" value="添加" />
</form>
</body>
</html>
View Code

1.跳转页面失败后如何把错误信息去掉的问题:

技术分享

2.在表单<form>里面的action指向哪里的问题:指向__SELF__或者__ACTION__

先var_dump(get_defined_constants(true));显示出系统常量,找到__SELF__和__ACTION__;

 __SELF和__ACTION__的区别:__SELF__访问的是自身,就是浏览器访问的地址,如:localhost/tp/index.php/Home/Main/Biaodan/code/10(code和10是随意传的参数),__ACTION__只能访问到Biaodan(方法)这里

技术分享

 

数据库访问CRUD;__SELF__和__ACTION__的区别;自动收集表单:$n->create();

标签:distinct   page   指定   namespace   使用   har   eth   png   自身   

原文地址:http://www.cnblogs.com/cuizhenyu/p/6213017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!