title: User registration with CodeIgniter
body: <p>I'm trying to build a registration system with CodeIgniter. I have a controller called <code>Register</code> with the following code:</p>
  
  <pre><code>class Register extends CI_Controller {
      public function index()
      {
          $this-&gt;load-&gt;helper(array('form', 'url'));
          $this-&gt;load-&gt;library('form_validation');
          $this-&gt;form_validation-&gt;set_error_delimiters('&lt;span class="error"&gt;', '&lt;/span&gt;');
          $this-&gt;form_validation-&gt;set_rules('username', 'username', 'required|min_length[3]|max_length[12]|trim');
          $this-&gt;form_validation-&gt;set_rules('password', 'password', 'required|min_length[2]|md5');
          $this-&gt;form_validation-&gt;set_rules('email', 'email', 'required|valid_email|trim');
          $this-&gt;form_validation-&gt;set_rules('artist', 'artist', 'max_length[32]|trim');
          $this-&gt;form_validation-&gt;set_rules('captcha', 'CAPTCHA', 'required|trim');
          $this-&gt;load-&gt;view('header');
          if(!$this-&gt;form_validation-&gt;run())
          {
              $this-&gt;load-&gt;view('register_form');
          }
          else
          {
              $this-&gt;load-&gt;view('register_done');
          }
          $this-&gt;load-&gt;view('footer');
      }
  }
  </code></pre>
  
  <p>So far so good. If I go to the register page I get the registration form displayed. If I send the form and it passes the form validation checks, I get the success page, if the form has errors, I get the form back with some error messages.</p>
  
  <p>Now what I want to do is the database stuff. I have some idea of how I can get the POST values from the registration form into my database, but no clue how I can check if a username or email already exists, and if so, display that error on the registration form. Here's my registration form view:</p>
  
  <pre><code>&lt;?php $this-&gt;load-&gt;helper('form'); ?&gt;
  &lt;?php echo form_open('register'); ?&gt;
      &lt;ul id="register"&gt;
          &lt;ul&gt;
              &lt;h3&gt;Account information&lt;/h3&gt;
              &lt;li&gt;
                  &lt;label for="username"&gt;Choose a username&lt;/label&gt;
                  &lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" /&gt;
                  &lt;span class="desc"&gt;The name you'd like to be known by&lt;/span&gt;
                  &lt;?php echo form_error('username'); ?&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;label for="password"&gt;Pick a password&lt;/label&gt;
                  &lt;input type="password" name="password" /&gt;
                  &lt;span class="desc"&gt;The best passwords are random and more than 6 characters long&lt;/span&gt;
                  &lt;?php echo form_error('password'); ?&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;label for="email"&gt;Enter your valid email address&lt;/label&gt;
                  &lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" /&gt;
                  &lt;span class="desc"&gt;We'll send you an activation email&lt;/span&gt;
                  &lt;?php echo form_error('email'); ?&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
          &lt;ul&gt;
              &lt;h3&gt;About you&lt;/h3&gt;
              &lt;li&gt;
                  &lt;label for="band"&gt;Who's your favorite artist?&lt;/label&gt;
                  &lt;input type="text" name="artist" value="&lt;?php echo set_value('artist'); ?&gt;" /&gt;
                  &lt;span class="desc"&gt;Don't put Lady GaGa.&lt;/span&gt;
                  &lt;?php echo form_error('artist'); ?&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
          &lt;ul&gt;
              &lt;h3&gt;Security question&lt;/h3&gt;
              &lt;li&gt;
                  &lt;label for="captcha"&gt;Enter the letters you see in the image&lt;/label&gt;
                  &lt;?php $this-&gt;load-&gt;helper('captcha');
                  $cap = create_captcha(array('img_path' =&gt; './captcha/', 'img_url' =&gt; 'http://localhost/captcha/', 'img_width' =&gt; 200, 'img_height' =&gt; 30));
                  $data = array('captcha_time' =&gt; $cap['time'], 'ip_address' =&gt; $this-&gt;input-&gt;ip_address(), 'word' =&gt; $cap['word']);
                  $query = $this-&gt;db-&gt;insert_string('captcha', $data);
                  $this-&gt;db-&gt;query($query);
                  echo $cap['image']; ?&gt;
                  &lt;input type="text" name="captcha" /&gt;
                  &lt;?php echo form_error('captcha'); ?&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
          &lt;ul&gt;
              &lt;h3 class="submit"&gt;
                  &lt;input type="submit" value="Register" /&gt;
              &lt;/h3&gt;
          &lt;/ul&gt;
      &lt;/ul&gt;
  &lt;?php echo form_close(); ?&gt;
  </code></pre>
  
  <p>As you can see, I'm taking advantage of the <code>form_error()</code> function of CI to display form errors right under the field, and I would like the "username already exists" error to also be displayed under the username field.</p>
  
  <p>Can anyone provide some help? Even a nudge in the right direction?</p>
  
  <p>Thanks!</p>