Welcome Guest, Not a member yet? Register   Sign In
Best way of retrieving primary key
#1

[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)


Messages In This Thread
Best way of retrieving primary key - by El Forum - 09-21-2010, 06:05 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 06:19 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 06:29 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 06:30 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 06:32 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 06:46 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 07:05 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 09:33 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 09:49 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 10:24 AM
Best way of retrieving primary key - by El Forum - 09-21-2010, 10:24 PM
Best way of retrieving primary key - by El Forum - 09-22-2010, 07:57 AM
Best way of retrieving primary key - by El Forum - 09-22-2010, 10:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB