title: joomla database select and insert query
body: <p>i am trying to insert another info to joomla (2.5.7) database after user is registered. The user chooses his usergroup and I want the insertion to happen only when the user is in a specific group. So I am trying to use this code to get the group data from the databse first to be used in the insert query. Now it is just a testing ground, later this retrieved value be used in if statement.</p>
  
  <p>This is the code:</p>
  
  <pre><code>function onUserAfterSave($user, $isnew, $success, $msg)
      {
      if ($isnew &amp;&amp; $success) {
    $db = &amp;JFactory::getDBO();
    $query = "SELECT #__k2_users.group FROM #__k2_users WHERE userID = ".$user['id'];
    $db-&gt;setQuery($query);
    $group = $db-&gt;loadResult();
  
    $db-&gt;setQuery( 'INSERT INTO #__user_profiles (ordering) VALUES ('.$group.')' );
    $db-&gt;query();
     if (!$db-&gt;query()) 
     {
      throw new Exception($db-&gt;getErrorMsg());
     }  
    }
      return $this-&gt;onAfterStoreUser($user, $isnew, $success, $msg);
  }
  </code></pre>
  
  <p>and this is the error I am getting upon the failed registration:</p>
  
  <pre><code>Column count doesn't match value count at row 1 SQL=INSERT INTO std13_user_profiles (ordering) VALUES ()
  </code></pre>
  
  <p>If I read it correctly, it means that the select statement is not returning anything but why? Thank you for your help.</p>
  
  <p>UPDATE:</p>
  
  <pre><code> if ($isnew &amp;&amp; $success) {
    $db = &amp;JFactory::getDBO();
    $userId   = JArrayHelper::getValue($user, 'id', 0, 'int');
    $query = "SELECT #__k2_users.group FROM #__k2_users WHERE userID = ".$userId;
    $db-&gt;setQuery($query);
    $group = $db-&gt;loadResult();
  
  
      $query2 = "INSERT INTO #__user_profiles (ordering) VALUES ('".$group."')";
      $db-&gt;setQuery($query2);
      $db-&gt;query();
     if (!$db-&gt;query()) 
     {
      throw new Exception($db-&gt;getErrorMsg());
     }  
    }
  </code></pre>
  
  <p>with this code, I don't get any errors and the user is registered and the values are inserted. However the $group is always 0 and based on the value is only 1 or 3 in k2_users table, I am guessing that it returns nothing. I think it may be because the registered user is not stored in the databse yet and it doesn't have his ID yet to look for the group?</p>
  
  <p>UPDATE2:</p>
  
  <pre><code>if ($isnew &amp;&amp; $success) {
  
      $count = JRequest::getVar('gender');
      if($count == 3) {
  
      $db = &amp;JFactory::getDBO();
      $alias = $user['name'];
      $table = array(
      ' '=&gt;'-', 'Š'=&gt;'S', 'š'=&gt;'s', 'Ð'=&gt;'Dj', 'Ž'=&gt;'Z', 'ž'=&gt;'z', 'C'=&gt;'C', 'c'=&gt;'c', 'C'=&gt;'C', 'c'=&gt;'c',
      'À'=&gt;'A', 'Á'=&gt;'A', 'Â'=&gt;'A', 'Ã'=&gt;'A', 'Ä'=&gt;'A', 'Å'=&gt;'A', 'Æ'=&gt;'A', 'Ç'=&gt;'C', 'È'=&gt;'E', 'É'=&gt;'E',
      'Ê'=&gt;'E', 'Ë'=&gt;'E', 'Ì'=&gt;'I', 'Í'=&gt;'I', 'Î'=&gt;'I', 'Ï'=&gt;'I', 'Ñ'=&gt;'N', 'Ò'=&gt;'O', 'Ó'=&gt;'O', 'Ô'=&gt;'O',
      'Õ'=&gt;'O', 'Ö'=&gt;'O', 'ě'=&gt;'e', 'Ù'=&gt;'U', 'Ú'=&gt;'U', 'Û'=&gt;'U', 'Ü'=&gt;'U', 'Ý'=&gt;'Y', 'Þ'=&gt;'B', 'ß'=&gt;'Ss',
      'à'=&gt;'a', 'á'=&gt;'a', 'â'=&gt;'a', 'ã'=&gt;'a', 'ä'=&gt;'a', 'å'=&gt;'a', 'æ'=&gt;'a', 'ç'=&gt;'c', 'è'=&gt;'e', 'é'=&gt;'e',
      'ê'=&gt;'e', 'ë'=&gt;'e', 'ì'=&gt;'i', 'í'=&gt;'i', 'î'=&gt;'i', 'ï'=&gt;'i', 'ð'=&gt;'o', 'ñ'=&gt;'n', 'ò'=&gt;'o', 'ó'=&gt;'o',
      'ô'=&gt;'o', 'õ'=&gt;'o', 'ö'=&gt;'o', 'ø'=&gt;'o', 'ù'=&gt;'u', 'ú'=&gt;'u', 'û'=&gt;'u', 'ý'=&gt;'y', 'ý'=&gt;'y', 'þ'=&gt;'b',
      'ÿ'=&gt;'y', 'R'=&gt;'R', 'r'=&gt;'r', " "=&gt;'-', '"'=&gt;'-'
  );
      $string = strtr($alias, $table);
      $alias_low = strtolower($string);
      $query = "INSERT INTO #__menu (menutype, title, alias,  path, link, type, published, level, component_id, access) VALUES ('stavebnici','".$user['name']."','".$alias_low."','".$alias_low."',
      'index.php?option=com_k2&amp;view=itemlist&amp;layout=user&amp;id=".$user['id']."&amp;task=user','component',1,1,10012,1)";
      $db-&gt;setQuery($query);
      $db-&gt;query();
     if (!$db-&gt;query()) 
     {
      throw new Exception($db-&gt;getErrorMsg());
     }
     }  
    }
  </code></pre>
  
  <p>OKAY! I got it working so now I can insert new menu every time a user is created, however th activation link is not created and the registration says that it failed. This is the error:</p>
  
  <pre><code>Duplicate entry '0-1-vojtech-plesner-' for key 'idx_client_id_parent_id_alias_language' SQL=INSERT INTO std13_menu (menutype, title, alias, path, link, type, published, level, component_id, access) VALUES ('stavebnici','Vojtěch Plešner','vojtech-plesner','vojtech-plesner', 'index.php?option=com_k2&amp;view=itemlist&amp;layout=user&amp;id=2789&amp;task=user','component',1,1,10012,1)
  </code></pre>
  
  <p>The client_id, parent_id and language have values of 1,1 and * abd they are in all the rows so why is it saying it is duplicate?</p>