PHP---TP框架---添加数据-----有三种方式
添加数据
添加数据有三种方式:
第一种:
<?php
namespace Home\Controller;//这个文件的命名空间
use Think\Controller;//use使用哪一个而命名空进。找Controller父类的文件
//http://localhost/ThinkPHP/index.php/Home/Diyi/Test
class DiyiController extends Controller
{
function Test()
{
$m = D("Info");
//添加数据
//1.使用数组
//造数组,造关联数组
$attr = array("Code"=>"p006","Name"=>"专户","Sex"=>"1","Nation"=>"n003","Birthday"=>"1998-09-08");//数据库的列名是大写就大写,是小写就是小写。
$m->add($attr);// add()方法的添加数据
}
}
显示的结果:

第二种:
//2.使用AR方式:
//数据库的表名对应的是类名;表里的一条数据对应的是一个对象;表里的每一个字段对应的是对象里的成员。比如,Info表对应到程序里就是class Info就是Info类,类里有很多成员他分别是表里的一些字段,
/* class Info //称为实体类,和数据库的表是对应的,它应设在数据库里,类名就是表名,成员对象就是表里的字段名
{
public $code;
public $name;
public $sex;
public $nation;
public $birthday;
}
$i = new Info();//造对象,对象就代表数据库里的一条数据
$i->code = "";
*/
例子:
<?php
namespace Home\Controller;//这个文件的命名空间
use Think\Controller;//use使用哪一个而命名空进。找Controller父类的文件
//http://localhost/ThinkPHP/index.php/Home/Diyi/Test
class DiyiController extends Controller
{
function Test()
{
$m = D("Info");
//添加数据
$m->Code = "p0010";
$m->Name ="忽悠";
$m->Sex = "0";
$m->Nation = "n002";
$m->Birthday ="1990-03-04";
$m->add();
}
}
显示结果:

第三种:
//3.自动收集表单 //打页面,实现添加 $m->create();//自动收集表单,创建出数据 $m->add();//把收集的表单添加到数据库
例子:
DiyiController.class.php
<?php
namespace Home\Controller;//这个文件的命名空间
use Think\Controller;//use使用哪一个而命名空进。找Controller父类的文件
//http://localhost/ThinkPHP/index.php/Home/Diyi/Test
class DiyiController extends Controller
{
function Test()
{
if(empty($_POST))
{
$this->display();
}
else //$post不为空提交到数据库
{
$m = D("Info");
$m->create();//自动收集表单,创建出数据
$m->add();//把收集的表单添加到数据库
}
}
}

Test.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="__ACTION__" 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>
显示的结果:


跳转页面
DiyiController.class.php
<?php
namespace Home\Controller;//这个文件的命名空间
use Think\Controller;//use使用哪一个而命名空进。找Controller父类的文件
//http://localhost/ThinkPHP/index.php/Home/Diyi/Test
class DiyiController extends Controller
{
function Test()
{
if(empty($_POST))
{
$this->display();
}
else //$post不为空提交到数据库
{
$m = D("Info");
$m->create();//自动收集表单,创建出数据
$bs = $m->add();//把收集的表单添加到数据库
if($bs)
{
$this->success("添加成功","Test");//"Test"代表跳转到哪个页面
}
else
{
$this->error("添加成功","Test");//success("","")error("","")跳转页面的方法
}
}
}
}
Test.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="__ACTION__" 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>
显示的结果:


可以更改跳转的等待时间

在这里直接添加参数,就可以更改跳转的等待时间

浙公网安备 33010602011771号