是什么阻止数据插入CI?

时间:2019-05-16 18:19:52

标签: php codeigniter insert

我已将CI框架设置为具有数据库连接,将其置于自动加载状态并创建了表单,但仍然没有任何内容插入数据库!

我尝试使用对象(类)和其他方式在数组中传递信息

if (isset($_POST['register-submit'])) {

$this->load->model('Registermodel');
$this->load->library('form_validation');
$this->form_validation->set_rules('register-username', 'Username', 'required');
$this->form_validation->set_rules('register-password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('register-password-repeat', 'confirm passphrase', 'required|min_length[6]|matches[register-password]');
$this->form_validation->set_rules('register-pin', 'pin', 'required|regex_match[/^[0-9]{6}$/]');

//If form validation was successful
if ($this->form_validation->run() == TRUE) {

  echo 'successfully registered!';

  //Add user to database
  $data = array(
    'ci_useruniqid'=> $_POST['register-uniqid'],
    'ci_userdate'=> $_POST['register-date'],
    'ci_useruid'=>  $_POST['register-username'],
    'ci_userpwd'=>  password_hash($_POST['register-password'], PASSWORD_DEFAULT),
    'ci_usermnemonic'=> $_POST['register-mnemonic'],
    'ci_usercurrentaddress'=> $_POST['register-address'],
    'ci_useraccount'=>  $_POST['register-account'],
    'ci_useraccountbalance'=> $_POST['register-account-balance'],
    'ci_userpin'=>  $_POST['register-pin'],
    'ci_userstatus'=> $_POST['register-status'],
    'ci_usertype'=> $_POST['register-type'],
    'ci_userinfo'=> $_POST['register-info'],
    'ci_userpgp'=>  $_POST['register-pgp'],
    'ci_usercurrency'=> $_POST['register-currency']
  );
  $this->RegisterModel->adduser($data);

  redirect("AuthController/loginview", "refresh");
}

我希望发生的是将数据(如上所示)插入数据库。我的实际结果是没有响应,甚至像在if语句中回声一样简单。

我的表结构:

ci_userid   int(11)
ci_useruniqid   
ci_userdate date
ci_useruid  
ci_userpwd  
ci_usermnemonic 
ci_usercurrentaddress   
ci_useraccount  
ci_useraccountbalance   decimal(12,8)
ci_userpin  
ci_userstatus   
ci_usertype 
ci_userinfo 
ci_userpgp  
ci_usercurrency

其余为文本,这是我的adduser模型:

  public function adduser($data) {



$insert = $this->db->insert('users', $data);

  }

1 个答案:

答案 0 :(得分:0)

由于评论时间太长,因此我向您提供准答案,以帮助您调试。

echo 'hello world <br><pre>';
        print_r($_POST);

        if (isset($_POST['register-submit'])) {

            $this->load->model('Registermodel');
            $this->load->library('form_validation');
            $this->form_validation->set_rules('register-username', 'Username', 'required');
            $this->form_validation->set_rules('register-password', 'Password', 'required|min_length[6]');
            $this->form_validation->set_rules('register-password-repeat', 'confirm passphrase', 'required|min_length[6]|matches[register-password]');
            $this->form_validation->set_rules('register-pin', 'pin', 'required|regex_match[/^[0-9]{6}$/]');

//If form validation was successful
            if ($this->form_validation->run() == TRUE) {

                echo 'successfully registered!';

//Add user to database
                $data = array(
                    'ci_useruniqid' => $_POST['register-uniqid'],
                    'ci_userdate' => $_POST['register-date'],
                    'ci_useruid' => $_POST['register-username'],
                    'ci_userpwd' => password_hash($_POST['register-password'], PASSWORD_DEFAULT),
                    'ci_usermnemonic' => $_POST['register-mnemonic'],
                    'ci_usercurrentaddress' => $_POST['register-address'],
                    'ci_useraccount' => $_POST['register-account'],
                    'ci_useraccountbalance' => $_POST['register-account-balance'],
                    'ci_userpin' => $_POST['register-pin'],
                    'ci_userstatus' => $_POST['register-status'],
                    'ci_usertype' => $_POST['register-type'],
                    'ci_userinfo' => $_POST['register-info'],
                    'ci_userpgp' => $_POST['register-pgp'],
                    'ci_usercurrency' => $_POST['register-currency']
                );
                $this->RegisterModel->adduser($data);
                echo 'success';
                //redirect("AuthController/loginview", "refresh");
            } else {
                echo validation_errors();
            }
        } else {
            echo 'register-submit... well... does not exist';
        }

请注意,将$this->input->post('somename');用于所有$_POST。例如假设register-uniqid不存在(因为表单验证不是必需的,则表单验证不会捕获它),您将得到未定义的索引错误;因此,您必须要做isset($_POST['register-uniqid']) ? $_POST['register-uniqid'] : null,而$this->input->post()会为您做到这一点。

现在,即使您进行了此修复,如果register-uniqid是绝对关键的(不能为null),也请确保表单验证将其覆盖为必填项。即使您可能有一些隐藏的字段,也并不意味着用户无法删除它们,并向该db列添加空值。我建议完全放弃隐藏字段,并将任何与用户无关的与用户无关的输入编码。