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

[eluser]Barwick[/eluser]
[quote author="Barwick" date="1352173316"]Okay, I got the below to write to the database. However, as you can see, I'm not passing any of my variables...how can I accomplish this?

ALSO, if I change the number values, so example change 30 to 45, it doesn't update the database as it should - or let me phrase, how I want it too.

Code:
public function add_subscription()
    {
        $data = array(
            'id' => $this -> input -> post('userID'),
            'email' => $this -> input -> post('userEmail'),
            'subscription' => $this -> input -> post('package')
        );

        $this->db->query("INSERT INTO subscriptions (id, email, subscription) VALUES (34, 65, 30) ON DUPLICATE KEY UPDATE id=34");
    }
[/quote]

Okay, i figured out the update issue...I had ON DUPLICATE KEY UPDATE id=34...which is the primary key that's causing the duplicate...needed to specify what the duplicate record was going to replace. Which in this case is the subscription. So I changed that.

How do I enter in the array of variables into this query though?? Anyone??


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



Theme © iAndrew 2016 - Forum software by © MyBB