I’m trying to develop a simple tracking system. What I’m trying to do is either insert the new record or update the record IF it matches the same campaign. I want to insert a new record if the user is triggered on a new campaign. Here is my current query that work fine.
INSERT INTO `tracking` (`email`,`ip`,`referrer`,`campaign`,`timestamp`)
VALUES ('$campaign[0]','$ip','$referrer','$campaign[1]','$timestamp')
ON DUPLICATE KEY UPDATE `last_timestamp` = '$timestamp'
My goal is if joe@bob.net triggers campaign1, then it will INSERT the record. If he tries campaign 1 again, it just updates the timestamp. Now when joe@bob.net triggers campaign2, it inserts an entirely new record.
So basically I’m trying to get it to INSERT only when the user triggers a new campaign. Otherwise, I want it to update the timestamp.
Any ideas or advice I’d really appreciate it!
Unless I’m missing something, all you need is a unique key put on the ’email’ and ‘campaign’ columns on your table:
ALTER TABLE
trackingADD UNIQUE KEYuk_email_campaign(email,campaign);