php - INSERT unless exist in other table -
i have users
table on wesite , want write function take email
column , insert diffrent table, mailing_list
, if email isn't exist in there.
for question can assume each table have columns: id
,username
, email
.
this current code:
$get_mails = $db->query("select `username`,`email`,`email_approved`,`ver` `users` ver='yes' && email_approved='1'"); while ($mails = $get_mails->fetch_assoc()) { if ($db->query("select `email` `mailing_list` email='".$mails['email']."'")->num_rows == 0) { $db->query("insert `mailing_list` values('','".$mails['email']."','".$mails['username']."')"); } }
as can see it's not efficient. there way in insert
query itself?
thank you.
one (kinda dirty) solution put unique index on email
field in mailing list
, prevent double entries won't have select query first.
edit, example:
$db->query ( "insert ignore `mailing_list` select '',email,username `users` ver = 'yes' , email_approved = '1'" );
Comments
Post a Comment