MySQL - Check if exists, if yes update - else insert - without unique ID?
<p>Basically, I have a table to log when users login and what's their latest IP.</p>

<p>To prevent duplicates, i use the following method:
<a href="http://i.stack.imgur.com/fJamf.png" rel="nofollow">screenshot</a></p>

<p>This works fine, to a certain degree. Sometimes, or actually plenty of times you can spam request it to log twice, or even more than twice. The requests are sent really rapidly, so this does not work very well.</p>

<p>I assume one way to speed it up, would be to have a simple SQL Query to search to perform this:</p>

<p>Search for the username... If it exists, update IP. If it does not exist, insert new column.</p>

<p>But, how I would do this? I have no idea. Most of the methods are using "ON DUPLICATE KEY" which does not work with text/blob.</p>

<p>Any help is greatly appreciated.</p>

<p>Code: <a href="http://pastebin.com/xuFmgFp0" rel="nofollow">http://pastebin.com/xuFmgFp0</a></p>

<pre><code>$result = $conpdo-&gt;prepare("SELECT * FROM `logs` WHERE `username` LIKE :username");
$result-&gt;execute(array(':username' =&gt; $_GET['username']));         
$info = $result-&gt;fetch(PDO::FETCH_ASSOC);

if(empty($info))
{
    $result = $conpdo-&gt;prepare("INSERT INTO `logs` (`id`, `ip`, `username`, `last_update`) VALUES (NULL, :ip, :username, '0000-00-00 00:00:00');");
    $result-&gt;execute(array(':username' =&gt; $_GET['username'], ':ip' =&gt; $_GET['ip']));  
}
else
{
    $result = $conpdo-&gt;prepare("UPDATE `logs` SET `ip` = :ip WHERE `logs`.`id` = :id");
    $result-&gt;execute(array(':ip' =&gt; $_GET['ip'], ':id' =&gt; $info['id']));       
}
</code></pre>