Christian
Christian

Reputation: 269

How do I fetch the data from database and then post it

This program will let you login first then what I need to do is to have something like edit their profile/info. I cannot fetch the data from the database, when I click the page for profile and nothing is shown.

MODEL

function login($username, $password) {  
    $this->db->select('username, password');
    $this->db->from('tblsec');
    $this->db->where('username', $username);
    $this->db->where('password', MD5($password));
    $this->db->limit(1);

    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        return $query->result();
    } else {
        return false;
    }
}
public function get_id($username) {
    $query = $this->db->query("SELECT * from tblsec WHERE username = '$username'");
    $r = $query->result();
    return $r;
}

VIEW

<?php foreach ($username as $user): ?>
    <div class="form-group">
        <label class="col-sm-2 control-label">Name</label>
        <div class="col-sm-5">
            <?php echo $user->firstname; ?> &nbsp;<?php echo $user->lastname; ?>
        </div>
    </div>
    <br>
    <div class="form-group">
        <label class="col-sm-2 control-label">Password</label>
        <div class="col-sm-5">
            **********
        </div>
    </div>
    <br>             

    <div class="form-group">
        <label class="col-sm-2 control-label">Last Name</label>
        <div class="col-sm-5">
            <?php echo $user->lastname; ?>
        </div>
    </div>
    <br>               
    <br>
    <div class="form-group">
        <label class="col-sm-2 control-label">Email</label>
        <div class="col-sm-5">
            <?php echo $user->email; ?>
        </div>
    </div>
    <br>
<?php endforeach; ?>
</div>
</div>

CONTROLLER

function __construct() {
    parent::__construct();
    $this->load->model('secretary_model', '', TRUE);
    $this->load->library('form_validation');
    $this->load->helper('date');
}

public function index() {
    if ($this->session->userdata('logged_in')) {
        $this->header();     
        $this->load->view('secretary/sec_login_view');
    } else {
        redirect('secretary/sec_login_view');
    }
}
public function profile() {
    if ($this->session->userdata('logged_in')) {
        $username = $this->session->userdata('username');
        $data['username'] = $this->secretary_model->get_ID($username);
        $this->header2();
        $this->load->view('secretary/secretary_profile', $data);
    } else {
        redirect('secretary/login', 'refresh');
    }
}

Upvotes: 1

Views: 164

Answers (1)

jogesh_pi
jogesh_pi

Reputation: 9782

First of all, Make sure you have set the logged_in session, that will ensure that the user is logged in or not. Something like

// After checking the username and password
$this->session->set_userdata('logged_in', true); 

Second If username or related data not found then return the false or appropriate error in model.

/**
 *@return mixed object|false
 */
function get_id($username) {
    $query = $this->db->query("SELECT * from tblsec WHERE username = '$username'");

    return $query->num_rows() > 0 ? $query->result() : false;
}

Then check in your view value in your view like this:

if( is_array($username) && count($username) > 0 ) {
    foreach ($username as $user): 
        // Set data accordingly 
    endforeach;
}
else {
    echo "Sorry! Related Data not found!";
}

Its a good approach to fetch and debug things accordingly.

Upvotes: 1

Related Questions