title: MySQL insert multiple rows from explode
body: <p>I have a variable</p>
  
  <pre><code>$allnames = 'John,Peter,Maya';
  </code></pre>
  
  <p>and I am trying to check if each name exists in the <code>user</code> table, and if it exists I want insert each name into one row in another table.</p>
  
  <pre><code>$allnames = 'John,Peter,Maya';
  $names = explode(",", $allnames);
  
  foreach($names as $name)
  {
      $checkusers = $db-&gt;query_read("SELECT username,userid FROM " . TABLE_PREFIX . " `user` WHERE username = '".$name."' "); 
      $results = $db-&gt;fetch_array($checkusers);
  
      $db-&gt;query_write("INSERT INTO " . TABLE_PREFIX . " `invite_users` (username)  VALUES ('".$results['userid']."') 
  "); 
  
  }
  </code></pre>
  
  <p>The solution :</p>
  
  <pre><code>$string = mysql_real_escape_string($_POST['userstag']);
  $arr = explode(",",$string);
  
  $sql = $db-&gt;query_read("SELECT username FROM  " . TABLE_PREFIX . " `user` WHERE username IN ('".implode("', '", $arr) . "')");
  
  while($r = $db-&gt;fetch_array($sql))
  {    
      $sqli = $db-&gt;query_write("INSERT INTO " . TABLE_PREFIX . " invite_users (username) VALUES ('".$r['username']."') 
      ");     
  }
  </code></pre>