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>
显示的结果:


可以更改跳转的等待时间

在这里直接添加参数,就可以更改跳转的等待时间
PHP---TP框架---添加数据-----有三种方式的更多相关文章
- Unity3D的按钮添加事件有三种方式
为Unity3D的按钮添加事件有三种方式,假设我们场景中有一个Canvas对象,Canvas对象中有一个Button对象. 方式一: 创建脚本ClickObject.cs,然后将脚本添加到Canvas ...
- tp框架之数据添加
1.数组添加 //$attr = array("Code"=>"n088","Name"=>"哈萨克族"); ...
- laravel7 ajax H-ui框架添加数据至数据库
1:定义路由: //租房 Route::resource('house','fang\FangattrController'); 2:控制器访问前端框架: public function create ...
- android80 HttpClient框架提交数据 get方式
package com.itheima.httpclient; import java.io.IOException; import java.io.InputStream; import java. ...
- TP框架 增删查
TP框架添加数据到数据库1.使用数组方式添加造模型对象 2.使用AR方式 强类型语言存在的方式 3.使用自动收集表单添加 :只能用POST方式,提交数据一个操作方法实现两个逻辑:A显示页面B得到数据 ...
- TP框架的增删改
TP添加数据有三种方式 1. //1.使用数组添加 $n = M("nation"); $arr = array("Code"=>"n007&q ...
- geotrellis使用(二)geotrellis-chatta-demo以及geotrellis框架数据读取方式初探
在上篇博客(geotrellis使用初探)中简单介绍了geotrellis-chatta-demo的大致工作流程,但是有一个重要的问题就是此demo如何调取数据进行瓦片切割分析处理等并未说明,经过几天 ...
- tp框架 :操作数据库
操作数据库,进行增删改数据 一.对数据表添加数据(方法:add()) (1)上一篇已经讲过链接数据库了,继续进行对数据库的操作,还是用控制器文件中的HomeController.class文件 看下数 ...
- TP框架对数据库的基本操作
数据库的操作,无疑就是连接数据库,然后对数据库中的表进行各种查询,然后就是对数据的增删改的操作,一步步的讲述一下框架对数据库的操作 想要操作数据库,第一步必然是要:链接数据库 一.链接数据库 (1)找 ...
随机推荐
- TCP Socket Establish;UDP Send Package Process In Kernel Sourcecode Learning
目录 . 引言 . TCP握手流程 . TCP connect() API原理 . TCP listen() API原理 . UDP交互过程 . UDP send() API原理 . UDP bind ...
- android jni
1, java.lang.UnsatisfiedLinkError: Couldn't load xxxxx: findLibrary returned null 当 apk 是被放到 /system ...
- MyEclipse使用SVN进行项目版本控制
一.搭建SVN服务器. 例如,使用VisualSVN Server,下载后安装. (1)在Repositories(版本库)上右击,新建Repository,选择Regular FSFS reposi ...
- while练习:输入一个班级的人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩。
Console.WriteLine("请输入班级的总人数:"); int count = int.Parse(Console.ReadLine()); ;//声明一个循环变量来记录 ...
- linux 软件安装
A:RPM包,这种软件包就像windows的EXE安装文件一样,各种文件已经编译好,并打了包,哪个文件该放到哪个文件夹,都指定好了,安装非常方便,在图形界面里你只需要双击就能自动安装,如果在命令行模式 ...
- spingMVC<1>-xml文件配置
---恢复内容开始---
- 新浪微博客户端(31)-显示相册图片上的GIF标记
DJStatusPhotoView.h #import <UIKit/UIKit.h> @class DJPhoto; @interface DJStatusPhotoView : UII ...
- 用firebug给firefox添加信任链接
在前文“firefox查看微信公众平台的数据分析时就出现不信任链接怎么办?”我们使用了导入证书的方法添加信任链接,有网友反映说证书导入不成功,这里用另外一种方法来实现:用firebug给firefox ...
- Parencodings(imitate)
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20679 Accepted: 12436 De ...
- Win7下判断当前进程是否以管理员身份运行
判断当前程序是否以管理员身份运行,代码如下: #include <iostream> #include <windows.h> using namespace std; // ...