0

I have my crud which is I want to upgrade it which is I want to echo the user who created another user account. This is my controller to add the user.

public function add()
{   
    $this->data['page_title'] = "Add User";

    $input_data = $this->input->post(); 

    if(!empty($input_data))
    {
        $this->User_model->insert($input_data);
        redirect('/users');
    } else {
        $this->load->view('templates/master', $this->data);
    }

}

Below is my model for populate the user information.

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class User_model extends CI_Model {

private $_table = "users";


public function get( $id = false )
{

    if ($id) {
        $this->db->where('id =', $id);
        $query = $this->db->get($this->_table);
        return $query->row_array();
    }

    $query = $this->db->get($this->_table);    
    return $query->result_array();
}

public function insert($data)
{ 
    $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
    $this->db->insert($this->_table, $data);

}

public function update($data)
{
    $id = $data['id'];
    unset($data['id']);
    $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
    $this->db->where('id =', $id);
    $this->db->update($this->_table, $data);
}

public function delete($id)
{
    $this->db->where('id', $id);
    $this->db->delete($this->_table);
}

public function login($username, $password){
    $query = $this->db->get_where('users', array('username'=>$username, $data['username'] = password_hash($data['password'],PASSWORD_DEFAULT))) ;

    return $query->row_array();
}

}

This is my text field(created by) view which is I want to auto echo the user who created the data.

<div class="col-sm-6">
      <label>Created By</label>
      <input type="text"required class="form-control form-control-user" name="created_by" placeholder="Created By"required>
      </div>

Mohsin Marui
  • 459
  • 2
  • 8
cdt
  • 135
  • 1
  • 1
  • 9

2 Answers2

0
<input type="text" class="form-control form-control-user" name="created_by" placeholder="Created By" value="<?php echo $fetch['created_by']; ?>" required>

I'm guessing the name is in the db same row as the data so than change $fetch['created_by'] to whatever variable you are using.

Mikeyhun
  • 256
  • 1
  • 8
  • Thank you sir , there still an error . which is i want to echo the user who is using at this session . is it possible ? – cdt Jan 14 '20 at 02:05
  • example if i'm log in the system it will automatically show to the created by textfield my username , so that if i create data it will automatically show my name as created by. – cdt Jan 14 '20 at 02:10
  • Yeah you can do that. You need to store your username in the session when you log in and echo that out. Need code for that as well or you can manage it? – Mikeyhun Jan 14 '20 at 11:27
  • yes sir , I need code sir so that i can see how it works. – cdt Jan 15 '20 at 05:17
  • yes sir pls if you have idea how? , I need code sir so that i can see how it works. – cdt Jan 16 '20 at 02:33
  • When you check if the username and pass match in your controller after you fetched the data from db, you just add `$_SESSION['username'] = $username;` and than in the input field you just echo out `$_SESSION['username']` instead. Or you can go with @Alie Muhammad way but than you can't change it if its needed. – Mikeyhun Jan 16 '20 at 12:19
0

you don't need to echo the user name on the html, just put directly on insert function on model;

just like this.

public function insert($data)
{ 
    $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
    $data['created_by'] = $this->session->name;
    $this->db->insert($this->_table, $data);

}

Note : your target table, have a 'created_by' column

Moh Ali
  • 71
  • 2