我在 Codeigniter 中创建了一个表单,但它不会向数据库提交值。我试图通过在我的有效方法中回显“hello”来调试我的代码,但实际方法不会向数据库提交任何内容。
这是我的视图页面中的代码
<div data-role="content">
<?php echo form_open('index.php/welcome/adduser'); ?>
<div data-role="fieldcontain">
<?php echo validation_errors('<p class="error">','</p>'); ?>
<p>
<label>Firstname: </label>
<?php echo form_input('firstname', set_value( 'firstname' ) ); ?>
</p>
<p>
<label>Lastname: </label>
<?php echo form_input('lastname', set_value( 'lastname' ) ); ?>
</p>
<p>
<label>Email: </label>
<?php echo form_input('email', set_value( 'email' ) ); ?>
</p>
<p>
<label>Age: </label>
<?php echo form_input('age', set_value( 'age' ) ); ?>
</p>
<p>
<label>username: </label>
<?php echo form_input('username', set_value( 'username' ) ); ?>
</p>
<p>
<label>Password: </label>
<?php echo form_input('password', set_value( 'password' ) ); ?>
</p>
<p>
<?php echo form_submit('submit', 'Submit'); ?>
</p>
</div>
这是我的控制器
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller {
/** loading services* */
function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
}
/** loading several pages * */
public function index() {
$this->load->view('abt-header');
$this->load->view('abt-abovetheblues');
$this->load->view('abt-footer');
}
public function adduser() {
if ($this->_adduser_validate() === FALSE) {
$this->index();
return;
}
$data = new user();
$data->firstname = $this->input->post('firstname');
$data->lastname = $this->input->post('lastname');
$data->username = $this->input->post('username');
$data->password = $this->input->post('password');
$data->email = $this->input->post('email');
$data->age = $this->input->post('age');
$this->load->view('#abt-profile');
}
private function _adduser_validate() {
// validation rules
$this->form_validation->set_rules('firstname', 'Firstname', 'required');
$this->form_validation->set_rules('lastname', 'Lastname', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[6]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[12]');
// $this->form_validation->set_rules('passconf', 'Confirm Password', 'required|matches[password]');
$this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
$this->form_validation->set_rules('age', 'Age', 'required');
return $this->form_validation->run();
}
}
?>