Home > Blockchain >  Unable to insert POST variables into database
Unable to insert POST variables into database

Time:11-02

I'm a beginner in CodeIgniter and I'm trying to insert the value that I'm getting from the post into my database, for which I'm using the following code:

View Class:

<form method="POST">
    <label>Username</label>
    <input type="text" name="username"/>
    <br/>
    <label>Password</label>
    <input type="password" name="password"/>
    <br/>
    <button type="submit" name="login">Sign In</button>
    </form>

Controller Class:

class Auth extends CI_Controller{
    public function index(){
        $this->load->model("membership_model");
        $username=$this->input->post('username');
        $password=$this->input->post('password');
        $content='';
        return $this->load->view('auth/login');
        if(isset($_POST["username"]) && isset($_POST["password"])){
            $content['data'] = $this->membership_model->create_user($username,$password);
        }
}

Model Class:

class Membership_model extends CI_Model {
         public function create_user($username,$password){
            $this->db->set('display_name',$username);
             $this->db->set('password',$password);
             $this->db->insert("table1");
             return true;
         }
     }

But when I hit submit, I do not get any data in my database for some reason.

Thanks in advance.

CodePudding user response:

The following code should work well.

Controller modification:

<?php

namespace App\Controllers;

use CodeIgniter\Controller;

    class Auth extends Controller{
        public function index(){
            if($this->input->method() == "post") {
                $this->load->model("membership_model");
                $username=$this->input->post('username');
                $password=$this->input->post('password');
                $content='';
                
                if(isset($_POST["username"]) && isset($_POST["password"])){
                    $content['data'] = $this->membership_model->create_user($username,$password);
                }
            }
            return $this->load->view('auth/login');
    }

Model modification:

public function create_user($username, $password){
    $data = array(
        'display_name'=>$username,
        'display_name'=>$password
    );
    $this->db->insert("table1", $data);
    return ($this->db->affected_rows() != 1) ? false : true;
}

CodePudding user response:

There are a number of reasons for data not entering your database, but yet not showing any errors. some of which include:

  • No action attribute in your form, thus the page just reloads. You should put the route to the controller that handles the data e.g '/auth` as your action atttribute
  • The database has some sort of constraint, that prevents data from entering it. For example, you might have accidentally put a NOT NULL constraint on one of your fields in the databse. Thus the record will not enter because that field is empty

CodePudding user response:

You didn't add any action end point when you submitting the form. So it's basically refreshing the page. So you should do

  1. specify the action in your with route url.
  2. And still if not solve check the database field and check the value you passing whether it's null or not.

CodePudding user response:

I think I see the problem. You are calling return $this->load->view('auth/login'); before the process that inserts data into your db. Try changing your

Controller Class from:

class Auth extends CI_Controller{
    public function index(){
        $this->load->model("membership_model");
        $username=$this->input->post('username');
        $password=$this->input->post('password');
        $content='';
        return $this->load->view('auth/login');
        if(isset($_POST["username"]) && isset($_POST["password"])){
            $content['data'] = $this->membership_model->create_user($username,$password);
        }
}

to:

class Auth extends CI_Controller{
    public function index(){
        $this->load->model("membership_model");
        $username=$this->input->post('username');
        $password=$this->input->post('password');
        $content='';
        if(isset($_POST["username"]) && isset($_POST["password"])){
            $content['data'] = $this->membership_model->create_user($username,$password);
        }
        return $this->load->view('auth/login');

}
  • Related