yii中如何创建一个表单接受数据,并存到数据库中?
展开全部
1. 确保数据是用户输入安全的(safe)
使用rules方法批量设置Model属性为输入安全的
[php] view plaincopy
public function rules() {
return array(
array('username, password, gender,
email, tel', 'safe'),
);
}
2. 如果设置了场景(Scenario),确保该场景匹配输入安全的规则
[php] view plaincopy
public function rules() {
......
array('gender, email, tel', 'safe', 'on'=> 'create,update'),
......
}
在属性设置前设置一下场景(Scenario),
[php] view plaincopy
...
$model->setScenario('create');
$model->attributes = $_POST['user'];
...
使用rules方法批量设置Model属性为输入安全的
[php] view plaincopy
public function rules() {
return array(
array('username, password, gender,
email, tel', 'safe'),
);
}
2. 如果设置了场景(Scenario),确保该场景匹配输入安全的规则
[php] view plaincopy
public function rules() {
......
array('gender, email, tel', 'safe', 'on'=> 'create,update'),
......
}
在属性设置前设置一下场景(Scenario),
[php] view plaincopy
...
$model->setScenario('create');
$model->attributes = $_POST['user'];
...
展开全部
controller方法
public function actionToAddTalk(){
$this->redirect('talk/index');
}
public function actionAddTalk2(){
$talk=new Talk;
$username=$_POST['username'];
$subject=$_POST['subject'];
$content=$_POST['content'];
$talk->subject=$subject;
$talk->username=$username;
$talk->content=$content;
$talk->save();
return $this->actionGetTalk();
}
执行完AddTalk将数据保存在数据库后跳转GetTalk方法
public function actionGetTalk(){//查找所有数据
$talk = Talk::model()->findAll(); //将数据表中全部信息存到 $talk
$this->renderPartial('admin',array('talk'=>$talk));
}
GetTalk将数据库保存的数据传给变量talk,将talk传给admin视图,在视图遍历输出我们提交的数据
public function actionToAddTalk(){
$this->redirect('talk/index');
}
public function actionAddTalk2(){
$talk=new Talk;
$username=$_POST['username'];
$subject=$_POST['subject'];
$content=$_POST['content'];
$talk->subject=$subject;
$talk->username=$username;
$talk->content=$content;
$talk->save();
return $this->actionGetTalk();
}
执行完AddTalk将数据保存在数据库后跳转GetTalk方法
public function actionGetTalk(){//查找所有数据
$talk = Talk::model()->findAll(); //将数据表中全部信息存到 $talk
$this->renderPartial('admin',array('talk'=>$talk));
}
GetTalk将数据库保存的数据传给变量talk,将talk传给admin视图,在视图遍历输出我们提交的数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询