<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model{
function __construct(){
$this->userTbl='login';
}
public function insert($data=array()){
if(!array_key_exists("created", $data)){
$data['created'] = date("Y-m-d H:i:s");
}
if(!array_key_exists("modified", $data)){
$data['modified'] = date("Y-m-d H:i:s");
}
$insert = $this->db->insert($this->userTbl, $data);
if($insert){
return $this->db->insert_id();;
}else{
return false;
}
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user');
}
/*
* User registration
*/
public function registration(){
$data=array();
$userData=array();
if($this->input->post(regisSubmit)){
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
$this->form_validation->set_rules('password', 'password', 'required');
$this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]');
}
$userData=array(
'name'=>strip_tags($this->input->post('name')),
'email'=>strip_tags($this->input->post('email')),
'password'=>strip_tags($this->input->post('password')),
'gender'=>strip_tags($this->input->post('gender')),
'phone'=>strip_tags($this->input->post('phone'))
);
if($this->form_validation->run()==true){
$insert=$this->user->insert($userData);
if($insert){
$this->session->set_userdata('success_msg', 'Your registration was successfully. Please login to your account.');
redirect(users/login);
}
else{
$data['error_msg']='Try again';
}
}
}
$data['user'] = $userData;
//load the view
$this->load->view('users/registration', $data);
}
<!DOCTYPE html>
<html lang="en">
<head>
<link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>
<div class="container">
<h2>User Registration</h2>
<form action="" method="post">
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" required="" value="<?php echo !empty($user['email'])?$user['email']:''; ?>">
<?php echo form_error('email','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone" value="<?php echo !empty($user['phone'])?$user['phone']:''; ?>">
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" required="">
<?php echo form_error('password','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="password" class="form-control" name="conf_password" placeholder="Confirm password" required="">
<?php echo form_error('conf_password','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<?php
if(!empty($user['gender']) && $user['gender'] == 'Female'){
$fcheck = 'checked="checked"';
$mcheck = '';
}else{
$mcheck = 'checked="checked"';
$fcheck = '';
}
?>
<div class="radio">
<label>
<input type="radio" name="gender" value="Male" <?php echo $mcheck; ?>>
Male
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="gender" value="Female" <?php echo $fcheck; ?>>
Female
</label>
</div>
</div>
<div class="form-group">
<input type="submit" name="regisSubmit" class="btn-primary" value="Submit"/>
</div>
</form>
<p class="footInfo">Already have an account? <a href="<?php echo base_url(); ?>users/login">Login here</a></p>
</div>
</body>
</html>
在此形式中,我需要使用codeigniter将数据插入mysql数据库。 但是数据没有插入到数据库中,也没有显示任何错误。下面是模态、控制器和视图页面的代码。在数据库.php文件中,令人毛骨悚然的事情很好。在这里,如何调试代码,以及错误是什么。提前谢谢。
- 在路由中定义 url.php
$route["用户/注册"] = "用户/注册";
-
还可以在 config/config.php 文件中定义您的项目base_url
-
用
表单操作="用户/注册" 方法="发布">
- 而不是
表单操作=" 方法="发布">
并查看文件
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
更改为:
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo isset($user['name']) ? $user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
您永远不会向控制器发布任何内容。 查看视图文件的这一行。
<form action="" method="post">
更改为 :
<form action="<?php echo base_URL(); ?>Users/registration" method="post">