Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t使用CodeIgniter将数据插入数据库_Php_Database_Forms_Codeigniter_Codeigniter 3 - Fatal编程技术网

Php Can';t使用CodeIgniter将数据插入数据库

Php Can';t使用CodeIgniter将数据插入数据库,php,database,forms,codeigniter,codeigniter-3,Php,Database,Forms,Codeigniter,Codeigniter 3,Iam使用最新版本的CI(3.1.6) 问题是我想要插入的数据不能发布到控制器,所以如果我从视图打印数据,它就不能从表单捕获任何数据。我认为问题出在表格上。但我不知道如何修复它。有人能帮我吗? 这是我的密码 My form.php代码 <div id="register" class="text-center"> <div class="container"> <div class="section-title center"> &l

Iam使用最新版本的CI(3.1.6) 问题是我想要插入的数据不能发布到控制器,所以如果我从视图打印数据,它就不能从表单捕获任何数据。我认为问题出在表格上。但我不知道如何修复它。有人能帮我吗? 这是我的密码

My form.php代码

<div id="register" class="text-center">
   <div class="container">
    <div class="section-title center">
      <h2>Register</h2>
      <hr>
      <form action="" method="post">
      <link href="<?php echo base_url();?>assets/user/css/login.css" rel="stylesheet">
      <input type="text" name="firstname" placeholder="First Name">
      <input type="text" name="lastname" placeholder="Last Name">
      <input type="text" name="fullname" placeholder="Full Name">
      <input type="text" name="username" placeholder="Username">
      <input type="text" name="email" placeholder="Email">
      <input type="password" name="pass" placeholder="Password">
   <!--    <input type="password" name="pass1" placeholder="Retype Password"> -->
      <ul>
      <p1>Gender</p1>
      <select name="gender" id="" class="pilihan">
        <option value="men">Male</option>
        <option value="women">Female</option></select>
      </ul>
      <ul>
      <p1>Occupation</p1>
      <li><input type="radio" name="job" value="doctor" checked> Doctor<br></li>
      <li><input type="radio" name="job" value="nurse"> Nurse<br></li>
    </ul>
      <link rel="stylesheet" type="text/css" href="assets/user/login.css">
      <a href="<?php echo base_url('c_register/do_insert'); ?>" class="btn btn-primary btn-lg active" role="button">Primary link</a>
  </form>

登记


要解决的第一个问题是模型的方法
InsertData()
。您已在模型中将其定义为需要两个参数-
$tableName
$data

public function InsertData($tabelName, $data){
但是在控制器中,您调用该方法而不提供任何参数

    } else {
        $this->m_register->InsertData(); //No parameters???
因此,您需要传递参数或更改方法的定义。这是一个简单得多的版本,可以完成插入作业,而无需更改控制器

public function InsertData()
{
    $datac = $this->input->post();
    return $this->db->insert('member', $datac);
}
您不需要一次一个地获取每个输入。在不传递字段名的情况下调用
input->post()
,将返回一个包含所有字段的数组。这意味着您不必手动创建
$datac
数组

此外,只返回
$this->db->insert
返回的内容,而不是将其保存到一个var然后返回var,这样就不需要太多输入

现在,为什么不显示新插入的数据?可能是因为在调用
$this->m_register->InsertData()之后您调用
$this->load->view('dashboard')但未为“仪表板”视图提供任何要显示的数据

我认为您真正想要做的是将
重定向到显示仪表板的控制器/方法。假设控制器是
Dashboard
,方法是
index()
do\u insert()
的最后一部分应该是这样的

if($this->form_validation->run() === FALSE)
{
    echo "gagal diinsert";
    // $this->load->view('templates/header', $data);
    // $this->load->view('news/comment_form');
    // $this->load->view('templates/footer');
}
else
{
    if($this->m_register->InsertData())
    {
        redirect('dashboard'); 
    }
    else
    {
        //show an error page or error message about the failed insert
    }
}
  function __construct(){
    parent::__construct();
    $this->load->model('m_register'); 
    $this->load->database();
}

您没有像这样在控制器构造函数上加载数据库类

if($this->form_validation->run() === FALSE)
{
    echo "gagal diinsert";
    // $this->load->view('templates/header', $data);
    // $this->load->view('news/comment_form');
    // $this->load->view('templates/footer');
}
else
{
    if($this->m_register->InsertData())
    {
        redirect('dashboard'); 
    }
    else
    {
        //show an error page or error message about the failed insert
    }
}
  function __construct(){
    parent::__construct();
    $this->load->model('m_register'); 
    $this->load->database();
}

您不需要添加模型。您可以使用以下简单方法:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class C_register extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('m_register');
    }


    function do_insert(){
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'firstname', 'required');
        $this->form_validation->set_rules('lastname', 'lastname', 'required');
        $this->form_validation->set_rules('fullname', 'fullname', 'required');
        $this->form_validation->set_rules('username', 'username', 'required');
        $this->form_validation->set_rules('email', 'email', 'required');
        $this->form_validation->set_rules('pass', 'pass', 'required');
        $this->form_validation->set_rules('gender', 'gender', 'required');
        $this->form_validation->set_rules('job', 'job', 'required');

        if ($this->form_validation->run() === FALSE) {
            echo "gagal diinsert";
        } else {
            //check in the array that it is same as column name in table like 'firstname' will be present on the table        
            $datac = array(
                'firstname' => $this->input->post('firstname'),
                'lastname' => $this->input->post('lastname'),
                'fullname' => $this->input->post('fullname'),
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'pass' => $this->input->post('pass'),
                'gender' => $this->input->post('gender'),
                'job' => $this->input->post('job')
            );

            $this->db->insert('member', $datac);
            $insert_id = $this->db->insert_id();
            if(!empty($insert_id)){
                $this->load->view('dashboard');
            }else{
                echo "failed";           
            }

        }
    }
}?>

我像你说的那样更改代码,但仍然无法将代码插入我的db@DFriend