thinkphp添加数据 add()方法

thinkphpz内置的add()方法用于向数据库表添加数据,相当于SQL中的INSERT INTO 行为
添加数据 add 方法是 CURD(Create,Update,Read,Delete / 创建,修改,读取,删除)中的 Create 的实现,ThinkPHP 支持以普通数组方式和面向对象方式将数据写入数据表。
//insert,以普通数组方式将数据写入数据表
$insert[‘user_id‘]=$my[‘user_id‘];
$insert[‘content_body‘]=$content;
$insert[‘posttime‘]=time();
$insert[‘replyid‘]=$sid;
$insert[‘type‘]=$type;
$insertid=$this->add($insert);
//由于文件位于contentModel.class.php中,所以$this<=>D("Content")
add() 方法如果添加数据记录成功,返回的是新数据记录主键,可直接得到

/***********以对象方式将数据写入数据表,在调用add方法写入数据的时候不需要传递参数*************/
public function insert(){
header("Content-Type:text/html; charset=utf-8");
$Dao = M("User"); // 实例化模型类
// 数据对象赋值
$Dao->username = "小王";
$Dao->password = md5("123456");
$Dao->email = "[email protected]";
$Dao->regdate = time();
// 写入数据
if($lastInsId = $Dao->add()){
echo "插入数据 id 为:$lastInsId";
} else {
$this->error(‘数据写入错误!‘);
} }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。