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.

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

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启