title: Comparing comma separated list to MySQL Table using PHP
body: <p>I have a HTML form that accepts a comma separated list of tags, which works just like the one here on stackoverflow. I also have a MySQL table that contains lists of tags that belong to each user. The user can update their tags in their profile, so what I would like to do is automatically remove any tags from the MySQL table if it no longer exists in the list</p>
  
  <p>For example, the user enters the following on the web form:</p>
  
  <pre><code>trance, house, electronica, dubstep, club
  </code></pre>
  
  <p>The MySQL table however, has this</p>
  
  <pre><code>userid    tag
  1         trance
  1         house
  1         electronica
  1         dubstep
  1         garage
  </code></pre>
  
  <p>I would like to remove the garage tag from MySQL and add "club" instead.</p>
  
  <p><strong>It is also worth noting</strong> that while these tags appear in order in my example above, this won't always be the case, so I can't rely on the array index position in both, for example.</p>
  
  <p>I realise I could probably loop through the comma separated list with a foreach and generate a delete statement, and re-insert all the tags from scratch but I feel that there is probably a better way to do this.</p>
  
  <p>Thanks in advance for your guidance.</p>
  
  <p>Dave</p>