duandong9195 2019-05-16 18:19
浏览 93
已采纳

什么阻止数据插入CI?

I have set the CI framework with database connection, put it on autoload and created a form, yet still, nothing is inserted into the Database!

I've tried using objects(classes) and different ways to pass information in an array

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");
}

What I expect to happen is for the data(as seen above) to be inserted into the DB. My actual result is no response even something as simple as echoing something out in an if statement.

My table structure:

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

The rest are text, here is my adduser model:

  public function adduser($data) {



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

  }
  • 写回答

1条回答 默认 最新

  • dozr162106 2019-05-17 03:53
    关注

    As this was too long for a comment, I present to you my quasi answer that will help you debug.

    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';
            }
    

    Please note, use $this->input->post('somename'); for all your $_POST stuff. e.g. assume that register-uniqid doesn't exist (form validation won't catch it because it isn't required) you'll get an undefined index error; thus you'd have to do isset($_POST['register-uniqid']) ? $_POST['register-uniqid'] : null whereas $this->input->post() does that logic for you.

    Now, even if you make this fix, if register-uniqid is absolutely critical (cannot be null) then make sure form validation covers it with a required. Even though you may have some hidden fields, it doesn't mean the user can't delete them if they want and post a null to that db column. I would suggest forgoing hidden fields entirely and coding any non-user-related input in to this controller or model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!