Clarification of what I'm trying to accomplish: Update "user name", if there is a record with the same e-mail but no name. if user already has a name, then don't make any changes
I have a website where all users can be "subscribers, entrepreneurs or investors". If a subscriber (of whom I have previously only asked for email) chooses to upload a business idea then that person will probably use the same e-mail address as before only this time adding a name. So I'd like to INSERT INTO, and if e-mail already exists - add the name, but only if there is not a name there already (so that a person cannot simply over write somebody else's details).
I've gotten this far:
mysql_query("
INSERT INTO users
(email, name)
VALUES
('" .$epost. "', '" .$namn. "')
ON DUPLICATE KEY UPDATE
name=VALUES(name) -->[if name == '', else do nothing...]
");
Now it replaces the current name with a new one if different.
I searched "on duplicate update if field empty" and found: http://forums.aspfree.com/sql-development-6/on-duplicate-key-update-but-only-if-value-isn-t-482012.html (merging?)
conditional on duplicate key update (closer, but I don't want to update it if it differs, only if the field is empty.)
http://bytes.com/topic/php/answers/914328-duplicate-key-update-only-null-values (if the input is blank.. well it's not)
http://boardreader.com/thread/Insert_on_duplicate_key_update_only_if_c_can8Xachr.html (the layout of this page got me lost)
http://www.digimantra.com/tutorials/insert-update-single-mysql-query-duplicate-key/ (it simply updates)
I believe this might be kind of what I'm after (?) http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql but I haven't managed to make it work
This code:
mysql_query("UPDATE users SET name='".$namn."'
WHERE email='".$epost."' AND name =''");
updates the name, only if it's previously null, and that's what I'm after however it does not insert a new record if email doesn't already exist.
My table
So I tried this:
mysql_query("
INSERT INTO users
SELECT email, 'victoria' FROM users
WHERE email='victoria@hejsan.se' ON DUPLICATE KEY UPDATE name = 'victoria'
");
and I tried this:
mysql_query("
INSERT INTO users
SELECT email, 'yay' from users
WHERE email='victoria@hejsan.se'
ON DUPLICATE KEY
UPDATE name = values(name)
");
from @Fluffeh 's answer
but nothing happens. Did i misinterpret the answer?
It just hit me that if someone already has an account then they should also have a password already, and so I'll just ask them to verify their password, and make sure I do an AJAX call while they insert their e-mail to see if the e-mail is already registered :) And so then this is no longer an issue, and I think that is the solution I'll go with. !)