Welcome Guest, Not a member yet? Register   Sign In
If user ID exists in database, UPDATE. Otherwise, INSERT (HELP)
#5

[eluser]Barwick[/eluser]
Valid points. No easy way about it...I respect your knowledge and help! Good for newbie's like myself trying to master this sh*t.

If I remove any primary or unique keys, I can update/insert fine in the database. Not even sure in this instance I need a primary (or would that be breaking a cardinal rule?).

However, I'm running an IF statement in my controller (pasted above & below which doesn't work) that should check the database to see if the userId exits, if it does, run the UPDATE function from the model. If it's doesn't, run the INSERT. But I can't seem to get this beast to work...or should I really just be doing the ON DUPLICATE KEY UPDATE function? not sure where to start with that though.

Anything stand out? Feel free to comment n the code for easier explanation...

Code:
public function update()
    {
        $userid = $this->session->userdata('user_id');  
        
        $this -> db -> where('id');
        $query = $this -> db -> get('subscriptions');
        
        if($query -> num_rows() == $userid) // Check to see if user id exists
        {
            // if subscription record found, update:
            $this -> subscribe_model -> update_subscription();
            redirect('dashboard');            
        }
        else
        {
            // if no records found, add:
            $this -> subscribe_model -> update_subscription();
            redirect('dashboard');  
        }
    }


Messages In This Thread
If user ID exists in database, UPDATE. Otherwise, INSERT (HELP) - by El Forum - 11-05-2012, 07:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB