[eluser]jeremydt[/eluser]
Hi All,
I've been working on this model for most of today and can't figure out what I'm doing wrong. Really hoping someone here can help me to figure it out.
Effectively, I store the logged in users username (user_username) in the session, but not the user_id.
User_id is the PK of a user table and a FK in the blog table (which stipulates the author of the blog). When an author posts a blog (they are logged in), I want to query the database for the user_id that matches the user_username in the session.
Everything works fine if I hardcode the user_id; all other fields are inserted correctly.
How would you do it?
Thanks in advance!
Code: function create_blog($filename) {
$username = $this->session->userdata('username');
$user_id = $this->get_user_id($username);
$new_blog_insert_data = array(
'blog_title' => $this->input->post('blog_title'),
'blog_content' => $this->input->post('blog_content'),
'blog_image' => $filename,
'blog_date' => date("Y-m-d H:i:s"),
'blog_active' => ($this->input->post('publish') == "Publish") ? '1' : '0',
'user_id' => $user_id
);
$insert = $this->db->insert('blog', $new_blog_insert_data);
return $insert;
}
Code: function get_user_id($username) {
$this->db->select('user_id');
$this->db->from('user');
$this->db->where('user_username', '$username');
$q = $this->db->get();
foreach ($q->result() as $row) {
return $row->user_id;
}
}
Quote:A Database Error Occurred
Error Number: 1048
Column 'user_id' cannot be null
INSERT INTO `blog` (`blog_title`, `blog_content`, `blog_image`, `blog_date`, `blog_active`, `user_id`) VALUES ('aaaa', 'aaaa', '4c989ed29bc57.jpg', '2010-09-21 22:02:26', '1', NULL)
[eluser]jeremydt[/eluser]
After hours of working on this, and minutes after posting; I found a solution. Please let me know if you know a better way though!
Code: function get_user_id($username) {
//$this->db->select('user_id');
//$this->db->from('user');
//$this->db->where('user_username', '$username');
//$q = $this->db->get();
//$q->result() as $row) {
// print_r($row->user_id);
//}
$query = $this->db->get_where('user', array('user_username' => $username));
foreach ($query->result() as $row)
{
return $row->user_id;
}
}
[eluser]Dennis Rasmussen[/eluser]
Quote:How would you do it?
I would store the user_id in the session 
Since user_id is your PK for most of your database tables, it would be smart to have it easily available.
[eluser]Alexandru M.[/eluser]
If the username is unique , then you can do something like :
Code: function get_user_id($username) {
$user_id = $this->db->get_where('user',array(user_name' => $username))->row();
return $user_id->user_id;
But why don't you just store it in the session on login ?
That would be easier
[eluser]jeremydt[/eluser]
Thanks,
I guess; I'll actually go ahead and integrate it with my membership code. Should be easy now I have a working function to retrieve it
In my existing membership class/model I query the database based on the user input on the login form; if it returns true then I just use the $this->input->post to set the session info. It came back to bite me!
[eluser]Dennis Rasmussen[/eluser]
So you are going to use the function instead of storing the user_id in the session?
If so, why?
[eluser]jeremydt[/eluser]
@Dennis
No, I'll now be integrating it into my session data. What I was trying to say is that I took a 'short-cut' earlier by using post to set my username. Now that I have the code to grab the userid from the database I'll migrate it into my login/membership class.
[eluser]Alexandru M.[/eluser]
Easiest way to do the whole login process would be to take the users input , select from the database the row (if any) and
Code: $this->session->set_userdata(array('user_id'=>$record->user_id,'username'=>$record->user_name,'user_level'=>$record->user_level));
or whatever you need for the logged in users.Then in each controller check for some session data , if its not set then the user is not logged in.I suggest putting the login code in a library or a model perhaps.
Hope I did not make any mistakes in the code ,I'm kinda tired at the moment.
[eluser]jeremydt[/eluser]
Thanks Alexandru.
This is what my login code currently looks like; I too am tired... perhaps I'll get around to integrating tomorrow.
login.php
Code: function validate_credentials() {
$this->load->model('model_membership');
$q = $this->model_membership->validate();
if($q) {
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('admin');
}
else {
$this->index();
}
}
model_membership.php
Code: function validate() {
$this->db->where('user_username', $this->input->post('username'));
$this->db->where('user_password', md5($this->input->post('password')));
$q = $this->db->get('user');
if($q->num_rows == 1) {
return true;
}
}
[eluser]Alexandru M.[/eluser]
In your model you can add this :
Code: function validate() {
$this->db->where('user_username', $this->input->post('username'));
$this->db->where('user_password', md5($this->input->post('password')));
$q = $this->db->get('user')->row();
if($q->num_rows == 1) {
$this->session->set_userdata('username',$q->user_username);
$this->session->set_userdata('user_id',$q->user_user_id); // adapt to your table
return TRUE;
}
else
return FALSE;
}
Then in the controller you call validate , if its false then redirect to index , else go to admin
|