If duplicate exists, I want to read all entries for that ID
I am writing a simple script to collect email addresses.
I want to collect ALL email addresses, even if they are duplicated.
However, I don't want to add the email address each time. I can identify that the email address is already registered and has an emailcount of 1 (this is in a separate field. The 1 is registered during the first registration)
My fields are:
|id |fname|surname|email |emailcount|
|23|John |Doe |email@example.com|1 |
When firstname.lastname@example.org is registered a second time, I want to read the whole line and pick out 'emailcount'.
I can then add 1 to the number and use UPDATE to modify it to the database.
This is the code that I have got up to now:
I would appreciate any help and advice.
The way I understand it you want to increase the emailcount column if it exists and if not add a new person. Thry this:
You can use the INSERT ON DUPLICATE KEY syntax.
I don't think you need the separate query to get the counter column *unless* the count is more than 1.
When you do the insert and do *NOT* get the duplicate key (meaning the counter will be 1), MySQL reports the number of rows affected as just 1. Whereas, when you do the insert and there is a duplicate, MySQL reports that 2 rows are affected. So you would only need to go inquire about the new count when the rows affected value is not 1.
It's a minor improvement, and if most of your inserts turn out to really *be* updates, then it's not worth the coding. But if, say, 75% of your inserts are new email addresses then it might be worth it.
Thank you very much for your suggestions.
I actually used 'sunfighter' suggestion. I understood it better.
However, I had a lot of work to do (for me) to get the script working how I wanted it to work.
The script is a 'double opt-in' script (I don't want false email addresses).
The original script had a $action=subscribe field sent from the form.
It then sent out an email to the given email address, with the required info to click on and send the info back to the script.
In order to use UPDATE feature, I had to include an $action=change in the validation email that was sent out to be clicked on.
I also used md5("$secret_code $formatted_email") as an extra validation.
Because I am a novice, it took me quite a while to get it right, but it now works.
Thank you very much.
|All times are GMT +1. The time now is 07:33 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.