我在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();
}
}
?>
答案 0 :(得分:0)
您没有指定要在哪个表中保存哪些数据。 使用这样的东西......
/*
$data = array(
'tableCol1' => $this->input->post('postedItem1'),
'tableCol2' => $this->input->post('postedItem2'),
);
*/
$this->db->set($data);
$this->db->insert($tableName);
答案 1 :(得分:0)
首先,您尚未关闭表单,请将其添加到您的视图中:
<?php echo form_close();?>
我没有看到您在哪里运行了将在数据库中插入数据的插入查询。你的Adduser功能可以是这样的:
public function adduser() {
if ($this->_adduser_validate() === FALSE) {
$this->index();
return;
}
$data = array(
'firstname' = $this->input->post('firstname'),
'lastname' = $this->input->post('lastname'),
'username' = $this->input->post('username'),
'password' = $this->input->post('password'),
'email' = $this->input->post('email'),
'age' = $this->input->post('age'),
);
$this->db->insert('user',$data);
$this->load->view('#abt-profile');
}
我假设您已在config / database.php文件中设置了数据库名称和登录凭据 这肯定会帮助你。如果有任何问题请告诉我。