title: Passing checkbox values to mysql database using Codeigniter
body: <p>I'm using CodeIgniter and mySQL to build a checkbox form. The form contains 4 options; each option has only one checkbox; users can select any combination of the options. I want to do the following:</p>
  
  <p>1 - For each checkbox, use a value of 1 (if unchecked) or 2 (if checked) and pass those values through to the database (each checkbox has its own field). Right now, whether checked or unchecked, the checkboxes are sending a value of 0 through to the database.</p>
  
  <p>2 - Once users update their checkboxes, I'd like to update the database to reflect the new values. Right now, a new row is added for each update to the checkboxes.</p>
  
  <p>What I've got so far is a form that submits the checkbox values to the database, a controller, and a model):</p>
  
  <p><strong>Form</strong></p>
  
  <pre><code>&lt;?php echo form_open('addFoo'); ?&gt;
  &lt;input type="checkbox" name="foo1" value="" /&gt;
  &lt;input type="checkbox" name="foo2" value="" /&gt;
  &lt;input type="checkbox" name="foo3" value="" /&gt;
  &lt;input type="checkbox" name="foo4" value="" /&gt;
  &lt;?php echo form_submit('submit', 'Save Changes'); ?&gt;
  &lt;?php echo form_close(); ?&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>function addFoo()
  {
      if ($this-&gt;input-&gt;post('submit')) {
          $id = $this-&gt;input-&gt;post('id');            
                  $foo1 = $this-&gt;input-&gt;post('foo1');
                  $foo2 = $this-&gt;input-&gt;post('foo2');
                  $foo3  = $this-&gt;input-&gt;post('foo3');
                  $foo4  = $this-&gt;input-&gt;post ('foo4');
  
      $this-&gt;load-&gt;model('foo_model');
      $this-&gt;foo_model-&gt;addFoo($id, $foo1, $foo2, $foo3, $foo4);
      }
  }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
          $data = array(
              'id' =&gt; $id,
              'foo1' =&gt; $foo1,
              'foo2' =&gt; $foo2,
              'foo3' =&gt; $foo3,
              'foo4' =&gt; $foo4
          );
  
          $this-&gt;db-&gt;insert('foo_table', $data);
      }
  </code></pre>