在CakePHP中更新现有记录
我想在CakePHP中创建以下内容: 起始页:用户输入数据库中预先存在的登录名。表名loginname。 一旦用户输入了正确的登录名,用户将继续使用注册表,用户可以在其中将个人数据添加到现有记录中 当用户保存表单时,只有loginname的记录会添加个人数据 这就是我所拥有的: 起始页的逻辑:在CakePHP中更新现有记录,php,cakephp,Php,Cakephp,我想在CakePHP中创建以下内容: 起始页:用户输入数据库中预先存在的登录名。表名loginname。 一旦用户输入了正确的登录名,用户将继续使用注册表,用户可以在其中将个人数据添加到现有记录中 当用户保存表单时,只有loginname的记录会添加个人数据 这就是我所拥有的: 起始页的逻辑: public function checkCodeRespondent() { //set var $password = $this->data['Respondent'
public function checkCodeRespondent() {
//set var
$password = $this->data['Respondent']['loginname'];
//set condition for hasAny function
$condition = array('Respondent.loginname'=>$password);
//if password matches continue to registreer
if($this->Respondent->hasAny($condition)){
//find data by password
$respondent = $this->findByPass($password);
//set var for current id
$id= $respondent['Respondent']['id'];
//redirect with current id
$this->redirect(array('action' => 'aanmelden', $id));
} else {
//if password does not match, return to index with errormessage
$this->redirect(array('action' => 'index'));
}
}
public function findByPass($pass) {
$respondent = $this->Respondent->find('first', array('conditions' => array('pass' => $pass)));
return $respondent;
}
这是将数据保存到记录的逻辑:
public function registreren() {
if($this->request->isPost()) {
$this->Respondent->id = $id;
if ($this->Respondent->save($this->request->data)){
$this->Flash->success(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
}
}
我以为我是通过函数checkCodeResponder()获得id的,但这不起作用。我忘记了什么或者我做错了什么?若我需要提供更多的信息,我当然乐意提供帮助
更新:我读到我需要添加
echo$form->input('id')代码>,仅当我调试($this->request)时代码>id值为空。我需要在代码中更改什么?如果您为id添加了隐藏字段,则只需替换:
$this->Respondent->id = $id;
作者:
$this->responder->id=$this->request->data['responder']['id'];
在您看来,在id的隐藏字段中指定name=“data[responder][id]”,如:
如果没有其他问题的话,这对您来说应该很好。在您正在执行的函数registeren()中,$this->responder->id=$id;这个$id是从哪里来的??或者您正在尝试将$id作为函数参数发送,如registereren($id)?我知道通常您会从url获取id,但我自己无法弄清楚,如何在不使用url的情况下获取id。我添加了一个id为的隐藏输入字段,这就是为什么我尝试使用$this->responder->id来检索id。但它不起作用。我错过了什么?该死,就是这么简单,哈哈。非常感谢你!非常感谢您抽出时间来帮助我:)
$this->Respondent->id = $this->request->data['Respondent']['id'];
And in your view, give name="data[Respondent][id]" in the hidden field of id like:
<input type="hidden" name="data[Respondent][id]" value="value of id">