Note that there are some explanatory texts on larger screens.

plurals
  1. POon duplicate entry add name if empty php mysql
    text
    copied!<p>Clarification of what I'm trying to accomplish: <strong>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</strong></p> <p>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).</p> <p>I've gotten this far:</p> <pre><code> mysql_query(" INSERT INTO users (email, name) VALUES ('" .$epost. "', '" .$namn. "') ON DUPLICATE KEY UPDATE name=VALUES(name) --&gt;[if name == '', else do nothing...] "); </code></pre> <p>Now it replaces the current name with a new one if different.</p> <p>I searched "on duplicate update if field empty" and found: <a href="http://forums.aspfree.com/sql-development-6/on-duplicate-key-update-but-only-if-value-isn-t-482012.html" rel="nofollow noreferrer">http://forums.aspfree.com/sql-development-6/on-duplicate-key-update-but-only-if-value-isn-t-482012.html</a> (merging?)</p> <p><a href="https://stackoverflow.com/questions/1044874/conditional-on-duplicate-key-update">conditional on duplicate key update</a> (closer, but I don't want to update it if it differs, only if the field is empty.)</p> <p><a href="http://bytes.com/topic/php/answers/914328-duplicate-key-update-only-null-values" rel="nofollow noreferrer">http://bytes.com/topic/php/answers/914328-duplicate-key-update-only-null-values</a> (if the input is blank.. well it's not)</p> <p><a href="http://boardreader.com/thread/Insert_on_duplicate_key_update_only_if_c_can8Xachr.html" rel="nofollow noreferrer">http://boardreader.com/thread/Insert_on_duplicate_key_update_only_if_c_can8Xachr.html</a> (the layout of this page got me lost)</p> <p><a href="http://www.digimantra.com/tutorials/insert-update-single-mysql-query-duplicate-key/" rel="nofollow noreferrer">http://www.digimantra.com/tutorials/insert-update-single-mysql-query-duplicate-key/</a> (it simply updates)</p> <p>I believe <strong>this might be</strong> kind of <strong>what I'm after</strong> (?) <a href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="nofollow noreferrer">http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql</a> but I haven't managed to make it work</p> <p>This code:</p> <pre><code> mysql_query("UPDATE users SET name='".$namn."' WHERE email='".$epost."' AND name =''"); </code></pre> <p>updates the name, only if it's previously null, and that's what I'm after <em>however</em> it does not insert a new record if email doesn't already exist.</p> <p>My table <img src="https://i.stack.imgur.com/eOvKH.png" alt="the table"></p> <p>So I tried this:</p> <pre><code> mysql_query(" INSERT INTO users SELECT email, 'victoria' FROM users WHERE email='victoria@hejsan.se' ON DUPLICATE KEY UPDATE name = 'victoria' "); </code></pre> <p>and I tried this:</p> <pre><code> mysql_query(" INSERT INTO users SELECT email, 'yay' from users WHERE email='victoria@hejsan.se' ON DUPLICATE KEY UPDATE name = values(name) "); </code></pre> <p>from @Fluffeh 's answer</p> <p>but nothing happens. Did i misinterpret the answer?</p> <p>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. !) </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload