Home > Backend Development > PHP Tutorial > ThinkPHP completes adding data and collecting form data_PHP tutorial

ThinkPHP completes adding data and collecting form data_PHP tutorial

WBOY
Release: 2016-07-13 09:54:53
Original
877 people have browsed it

ThinkPHP completes adding data and collecting form data

There are two ways to add data, one is the array method, and the other is adding using AR. In fact, the adding methods of these two methods are the same, it is just a standard difference. The so-called AR is :

//A data model represents a table.
//An object represents a piece of data.
//A field represents an attribute of an object

This is also in line with traditional thinking habits. The following is the example code added for data simulation:

		$temp=D('User');
		// $arr=array('id'=>11,'name'=>'add_1_xuning','password'=>md5('123456'));
		//下面是通过AR的方式进行数据添加
		//一个数据模型代表一张表。
		//一个对象代表一条数据。
		//一个字段代表一个对象的属性
		$temp->name="add_xuning";
		$temp->password=md5('456');
		$res=$temp->add();
		// $res=$temp->add($arr);
		if($res){
			$info=$temp->select();
			$this->assign('info',$info);
			$this->display();
		}else{
			echo "添加失败";
		}
	}
Copy after login

Then just collect the form data and add it:

1: {$smarty.const.__SELF__} represents the controller path of the current asking price.

2: There are three ways to collect data:

public function add_data(){
		if(!empty($_POST)){
			$arr=$_POST;
			$temp=D('User');
			//$res=$temp->add($arr);//直接添加
			// $temp->id=$_POST['id'];//AR添加
			// $temp->name=$_POST['usename'];
			// $temp->password=md5($_POST['password']);
			$temp->create();//create添加
			$res=$temp->add();
			if($res){
				echo "添加成功";
			}else{
				echo "添加失败";
			}
		}else{
			echo "内容为空";
			$this->display();
		}
	
	}
Copy after login
Among them, if you have special requirements for the third data, you can use the second method. If it is efficient and safe, use the third method. Generally, it is not recommended to use the second method.

In this case, the addition of data is completed

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/993869.htmlTechArticleThinkPHP completes the addition of data and the collection of form data. There are two ways to add data. One is the array method. , the other is to use AR to add, in fact, these two ways to add...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template