php - How do I restrict mysql UPDATE to just the row I'm dealing with inside a for loop? -


i have web app send btw 1 , 20 notification emails every time user submits form. user in group (they'll have groupid number) , people in group notification email.

note: sending emails using phpmailer , smtp via amazon ses.

background: if there 10+ emails, take 5 seconds send before showing user "successful form submission" message.

so i've decided send email in background improve ux, using cron job run every minute.

i created simple additional mysql table called "cron_email_notify" fields for:

  • groupid (id of group user in)
  • sent (0 default, updated 1 after cron-job runs)

in cron script run every minute, search groupids have sent set 0 (meaning still need notification emails). loop thru , send emails , update sent 1 after each loop.

the issue if more 1 user same group submit form before cron-job runs, there 2 (or more) entries same groupid , sent set 0.

if update sent 1 groupid using code below, update rows groupid. , 1 notification email sent when maybe 3 or 4 should have been sent.

$stmt = $pdo->prepare("update cron_email_notify set sent = 1 groupid = ? "); $stmt->execute([$poolid]); 

what proper way update groupid row i'm looping through? simple adding "limit 1" end of update query? or there more proper way it?

if understand correctly, not have primary key set on table. add new column named e.g. mailid , make primary key auto increment. each time update row, use column define row want update

$stmt = $pdo->prepare("update cron_email_notify set sent = 1 mailid = ? "); $stmt->execute([$mailid]); 

also might want keep track on date/time mail sent, example replacing sent senttime, make integer (if want use unixtime) or datetime. once mail sent, set value current time. mails have not been sent have default value, or null set (by can find them)

another option check if combination groupid sent = 0 exists in table. if so, don't insert... why send group 2 mails?


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -