在cakephp中插入数据
我有5个输入字段,当用户填写这5个字段并点击提交时,我们需要将它们存储在数据库中 为此,我的控制器中有此代码在cakephp中插入数据,php,mysql,cakephp,cakephp-2.x,Php,Mysql,Cakephp,Cakephp 2.x,我有5个输入字段,当用户填写这5个字段并点击提交时,我们需要将它们存储在数据库中 为此,我的控制器中有此代码 public function send() { $this->loadmodel('Invitefriend'); $this->request->is('post'); $this->Invitefriend->create(); $this->loadModel('Student'); $id = $this->userValue['St
public function send()
{
$this->loadmodel('Invitefriend');
$this->request->is('post');
$this->Invitefriend->create();
$this->loadModel('Student');
$id = $this->userValue['Student']['id'];
$contact = $this->userValue['Student']['phone'];
$emails= $this->data['Invitefriends'];
$currentDate=date('Y-m-d',strtotime($this->currentDate));
$email_count = count($emails);
for($i=1;$i<=$email_count;$i++)
{
$email = $emails['email'.$i];
$this->Invitefriend->save(array("ref_student_id"=>$id, "ref_contact"=>$contact, 'email'=>$email, 'date'=>$currentDate));
}
}
公共函数send()
{
$this->loadmodel('Invitefriend');
$this->request->is('post');
$this->Invitefriend->create();
$this->loadModel('Student');
$id=$this->userValue['Student']['id'];
$contact=$this->userValue['Student']['phone'];
$emails=$this->data['Invitefriends'];
$currentDate=date('Y-m-d',strottime($this->currentDate));
$email\u count=计数($email);
对于($i=1;$iInvitefriend->save(数组(“ref\u student\u id”=>$id,“ref\u contact”=>$contact,'email'=>$email,'date'=>$currentDate));
}
}
With只在数据库中插入最后一个字段值,但我需要将所有5个字段都存储到数据库中。我的代码中有什么错误吗?使用
// save multiple or all records
$this->Invitefriend->saveAll()
or
$this->Invitefriend->saveMany()
而不是
Invitefriend->save()
试试这个:
//Create entity for table...
$invTable = $this->Invitefriend->newEntity();
//Build db table fields...
$invTable->ref_student_id = $id;
$invTable->ref_contact = $contact;
$invTable->email = $email;
$invTable->date = $currentDate;
//Store data into table...
$this->Invitefriend->save($invTable);
你看起来像:
public function send() {
$this->loadmodel('Invitefriend');
$this->request->is('post');
$this->Invitefriend->create();
$this->loadModel('Student');
$id = $this->userValue['Student']['id'];
$contact = $this->userValue['Student']['phone'];
$emails= $this->data['Invitefriends'];
$currentDate=date('Y-m-d',strtotime($this->currentDate));
$email_count = count($emails);
//Create entity for table...
$invTable = $this->Invitefriend->newEntity();
//Build db table fields...
$invTable->ref_student_id = $id;
$invTable->ref_contact = $contact;
$invTable->date = $currentDate;
for($i=1;$i<=$email_count;$i++) {
$invTable->email = $emails['email'.$i];
//Store data into table...
$this->Invitefriend->save($invTable);
}
}
公共函数send(){
$this->loadmodel('Invitefriend');
$this->request->is('post');
$this->Invitefriend->create();
$this->loadModel('Student');
$id=$this->userValue['Student']['id'];
$contact=$this->userValue['Student']['phone'];
$emails=$this->data['Invitefriends'];
$currentDate=date('Y-m-d',strottime($this->currentDate));
$email\u count=计数($email);
//为表创建实体。。。
$invTable=$this->Invitefriend->newEntity();
//生成数据库表字段。。。
$invTable->ref\u student\u id=$id;
$invTable->ref_contact=$contact;
$invTable->date=$currentDate;
对于($i=1;$iemail=$emails['email.$i];
//将数据存储到表中。。。
$this->Invitefriend->save($invTable);
}
}