1

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
    Can you show your code of `adduser` function of `RegisterModel` file? Also show the table SS. – Rohit Mittal May 16 '19 at 18:26
  • Sure, give me a minute! – ignitingyourcode May 16 '19 at 18:28
  • Just FYI, if you name your form inputs that have the data to be inserted like `name="data[ci_useruniqid]"` then your code would just be `$this->RegisterModel->adduser($_POST['data']);` – AbraCadaver May 16 '19 at 18:32
  • If you can't echo something from an if statement, the assumption is that it's not true. Have you checked for PHP errors in your logs? – miken32 May 16 '19 at 18:35
  • @miken32 I'm aware of that, I've tried different ones with both `else` and `else if` statements, both show nothing. – ignitingyourcode May 16 '19 at 18:38
  • Then you're likely getting a parse error somewhere. https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – miken32 May 16 '19 at 18:42
  • You can print last executed query after `$this->RegisterModel->adduser($data);` using `echo $this->db->last_query();` check this query and run directly in mysql. Check if there is any error. – Rohit Mittal May 16 '19 at 18:52
  • @RohitMittal it won't echo out anything. – ignitingyourcode May 16 '19 at 18:58
  • Then you need to debug your code on every line. You can try to do die with comment just after if condition and check if code is reachable at that point. You need to further use this for complete code. – Rohit Mittal May 16 '19 at 19:00
  • It won't even die when I tell it to when I press the button! – ignitingyourcode May 16 '19 at 19:04
  • Could we see all the code for the controller, please? There might be something you didn't share that affects the results. – DFriend May 17 '19 at 01:54
  • troubleshooting in CI 101: (1) enabled `db_debug` in `database.php`, (2) make sure prod environment in `index.php` is set to development (to enable error reporting), (3) comment or remove *redirects*. Then try your code again, if it is still blank add an echo at the beginning of the controller function. If you still can't see it, idk what to tell you. – Alex May 17 '19 at 03:46

1 Answers1

0

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.

Alex
  • 9,215
  • 8
  • 39
  • 82
  • Thank you! I'm new to CI and one of the things I thought was going to be easier was debugging, glad I asked this question :-) – ignitingyourcode May 19 '19 at 20:33
  • Another thing, every time I reload it inserts again, why? – ignitingyourcode May 19 '19 at 20:41
  • typically: form submits to a processor and the processor either redirects back to the form on a failure, or to a success page. the form and processor and success page can be integrated, but that does not appear to be what you have. you need the redirect to solve your loop and bear in mind that given your scheme, you'll have to use flash messages (or change to ajax or a 1 pager) to get your *expected* error messages e.g. form validation. and for future "not working" is never a good starting point - it shows a lack of troubleshooting and is generally downvoted. – Alex May 20 '19 at 06:36