【问题标题】:form data into database using codeigniter使用codeigniter将数据形成数据库
【发布时间】:2018-09-04 11:03:40
【问题描述】:

<?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 数据库。但数据没有插入数据库,也没有显示任何错误。下面是模态、控制器和视图页面的代码。在 database.php 文件中,一切都很好。在这里,如何调试代码,以及错误是什么。提前致谢。

【问题讨论】:

  • “视图”中您的操作字段为空。在此处传递正确的注册 URL
  • 这与 laravel 有什么关系?

标签: php laravel codeigniter


【解决方案1】:
  • 在 routes.php 中定义 url

$route['user/registration'] = '用户/注册';

  • 还在 config/config.php 文件中定义你的项目 base_url

  • 使用

form action="用户/注册" 方法="发布">

  • 而不是

form action="" method="post">

【讨论】:

    【解决方案2】:

    并查看文件

    <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>'); ?>
    

    【讨论】:

      【解决方案3】:

      您从不向控制器发布任何内容。查看视图文件的这一行。

      <form action="" method="post">
      

      改为:

      <form action="<?php echo base_URL(); ?>Users/registration" method="post">
      

      【讨论】:

        猜你喜欢
        • 2014-05-17
        • 2013-04-04
        • 2016-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多