views:

42

answers:

1

Hi,

Can someone explain a simple solution using MySQL and PHP how to check if an email has been sent already, stopping duplicates for users.

I have a basic script, but it's not working for some reason - http://pastebin.com/k7yiQahb It inserts into the table the following:

feed_id, recipient_id, issent
0, 0, Y

Regards

A: 

Why not create a column issent of type enum(Yes,No) DEFAULT 'No' in the table recipients instead of a separate table.

Then, when email is sent successfully, run this:

 update `recipients` set issent = 'Yes' where id = $id

And when you are fetching email recipients in the beginning, just do:

 select email, suburb, id FROM recipients where issent = 'No' GROUP BY id ORDER BY id DESC

This will give you only unsent addresses.

[EDIT]: If there are multiple recipients, you can run first query like this:

 update `recipients` set issent = 'Yes' where id IN (3,4,5,6)
shamittomar
Thanks alot shamittomar.
Dean
However, the messages are one-to-many, so each message MAY be sent to several recipients. Which means this way is flawed, right?
Dean
@Dean, I have updated the answer for your case.
shamittomar
Well, it seems to run error free. However emails arent being sent. When run manually, its #1054 - Unknown column '$id' in 'where clause'
Dean
Just realised this wont work because each recipient will be getting multiple emails from time to time based on feed.id.
Dean
Uhh, you have to replace $id with whatever variable you're having. Use this: `mysql_query("update recipients set issent = 'Yes' where id = '{$recipient_id}' ");`
shamittomar