2016-09-08 172 views
-3

我想用cakephp將我的表單數據插入到數據庫中。我在layouts文件夾中創建了form.ctp文件。現在如何在form.ctp文件中將表單值插入數據庫?將表單數據插入到cakephp的數據庫中

這裏是我的代碼:

<html> 
<form action="../users/register" method="post"> 
<p>Please fill out the form below to register an account.</p> 
<label>Username:</label><input name="username" size="40" /> 
<label>Password:</label><input type="password" name="password" size="40" 
/> 
<label>Email Address:</label><input name="email" size="40" 
maxlength="255" /> 
<label>First Name:</label><input name="first_name" size="40" /> 
<label>Last Name:</label><input name="last_name" size="40" /> 
<input type="submit" value="register" /> 
</form> 
</html> 
+4

按照CakePHP的文檔保存而不改變表格的數據,你會明白。邏輯將在您的方法中處理。 – aavrug

回答

0
<html> 
<form action="../users/register" method="post"> 
<p>Please fill out the form below to register an account.</p> 
<label>Username:</label><input name="users[username]" size="40" /> 
<label>Password:</label><input type="users[password]" name="password" size="40" /> 
<label>Email Address:</label><input name="users[email]" size="40" 
maxlength="255" /> 
<label>First Name:</label><input name="users[first_name]" size="40" /> 
<label>Last Name:</label><input name="users[last_name]" size="40" /> 
<input type="submit" value="register" /> 
</form> 
</html> 

if ($this->request->is('post')) { 
    $this->User->save($this->request->data); 
} 

OR 節省

if ($this->request->is('post')) { 
    $this->request->data['User'] = $this->request->data; 
    $this->User->save($this->request->data); 
} 
相關問題