title: How do I insert data into a table using the Code Igniter framework?
body: <p>I am trying to connnect to my database on my local machine using code igniter. I views running, but am recieving the following error:</p>
  
  <pre><code>A PHP Error was encountered
  Severity: Notice
  Message: Undefined variable: client
  Filename: views/client.php
  Line Number: 1
  </code></pre>
  
  <p>The second error I am recieving is as follows:</p>
  
  <pre><code>A PHP Error was encountered
  Severity: Notice
  Message: Trying to get property of non-object
  Filename: views/client.php
  Line Number: 1
  </code></pre>
  
  <p>I went to check my database in mySQL Workbench and there is no data entered into database. My CORE model's code is as follows:</p>
  
  <pre><code>&lt;?php
  
  class MY_Model extends CI_Model {
  const DB_TABLE = 'abstract';
  const DB_TABLE_PK = 'abstract';
  
  /**
   * Create record.
   */
  private function insert() {
      $this-&gt;db-&gt;insert($this::DB_TABLE, $this);
      $this-&gt;{$this::DB_TABLE_PK} = $this-&gt;db-&gt;insert_id();
  }
  
  /**
   * Update record.
   */
  private function update() {
      $this-&gt;db-&gt;update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
  }
  
  /**
   * Populate from an array or standard class.
   * @param mixed $row
   */
  public function populate($row) {
      foreach ($row as $key =&gt; $value) {
          $this-&gt;$key = $value;
      }
  }
  
  /**
   * Load from the database.
   * @param int $id
   */
  public function load($id) {
      $query = $this-&gt;db-&gt;get_where($this::DB_TABLE, array(
          $this::DB_TABLE_PK =&gt; $id,
      ));
      $this-&gt;populate($query-&gt;row());
  }
  
  /**
   * Delete the current record.
   */
  public function delete() {
      $this-&gt;db-&gt;delete($this::DB_TABLE, array(
          $this-&gt;DB_TABLE_PK=&gt;$this-&gt;{$this::DB_TABLE_PK}
      ));
      unset($this-&gt;{$this::DB_TABLE_PK});
  }
  
  /**
   * Save the record.
   */
  public function save() {
      if (isset($this-&gt;{$this::DB_TABLE_PK})) {
          $this-&gt;update();
      }
      else {
          $this-&gt;insert();
      }
  }
  
  /**
   * Get an array of Models with optional limit, offset.
   * 
   * @ param int $limit Optional.
   * @ param int $offset Optional; if set, requires $limit.
   * @ return array Models populated by database, keyed by PK.
   */
  public function get($limit = 0, $offset = 0) {
      if($limit) {
          $query = $this-&gt;db-&gt;get($this::DB_TABLE, $limit, $offset);
      }
      else {
          $query = $this-&gt;db-&gt;get($this::DB_TABLE);
      }
      $ret_val = array();
      $class = get_class($this);
      foreach($query-&gt;result as $row) {
          $model = new $class;
          $model-&gt;populate($row);
      $ret_val[$row-&gt;{$this::DB_TABLE_PK}] = $model;
      }
      return $ret_val;
  }
  }
  </code></pre>
  
  <p>My client model is as follows: </p>
  
  <pre><code>&lt;?php
  
  class Clients extends MY_Model {
  const DB_TABLE = 'client';
  const DB_TABLE_PK = 'client_id';
  
  /**
   * Client's unique identifier.
   * @var int
   */
  public $client_id;
  
  /**
   * Client or representative's first name.
   * @var varchar(45)
   */
  public $first_name;
  
  /**
   * Client or representitive's last name.
   * @var varchar(45)
   */
  public $last_name;
  
  /**
   * Client's address.
   * @var varchar(45)
   */
  public $address;
  
  /**
   * Client's apartment or suite number.
   * @var int
   */
  public $apt_or_suite_number;
  
  /**
   * Client's postal zip code.
   * @var int
   */
  public $zip_code;
  
  /**
   * Client's city.
   * @var varchar(45)
   */
  public $city;
  
  /**
   * Two letter state abbreviation of client's state.
   * @var varchar(2)
   */
  public $state;
  
  /**
   * Client's email address.
   * @var varchar(45)
   */
  public $email;
  }
  </code></pre>
  
  <p>My registration controller thus far is:</p>
  
  <p>
  
  <pre><code>if (!defined('BASEPATH'))
  exit('No direct script access allowed');
  
  include_once("analyticstracking.php");
  
  class Registration extends CI_Controller {
  
  public function index() {
      /**
       * Load Models
       */
      $data = array();
  
      $this-&gt;load-&gt;model('Clients');
      $this-&gt;load-&gt;model('Phones');
      $client = new Clients();
      $data['first_name'] = $client;
      $this-&gt;load-&gt;view('client');
  
      /**
       * Validation
       */
      $this-&gt;load-&gt;library('form_validation');
      $this-&gt;form_validation-&gt;set_rules(array(
          array(
              'field' =&gt; 'first_name',
              'label' =&gt; 'First Name',
              'rules' =&gt; 'required',
          ),
          array(
              'field' =&gt; 'last_name',
              'label' =&gt; 'Last Name',
              'rules' =&gt; 'required',
          ),
          array(
              'field' =&gt; 'address',
              'label' =&gt; 'Address',
              'rules' =&gt; 'required',
          ),
          array(
              'field' =&gt; 'apt_or_suite_number',
              'label' =&gt; 'Apartment or Suite Number',
              'rules' =&gt; '',
          ),
          array(
              'field' =&gt; 'zip_code',
              'label' =&gt; 'Postal Zip Code',
              'rules' =&gt; 'required|is_numeric',
          ),
          array(
              'field' =&gt; 'city',
              'label' =&gt; 'City',
              'rule' =&gt; 'required',
          ),
          array(
              'field' =&gt; 'state',
              'label' =&gt; 'State',
              'rules' =&gt; 'required',
          ),
          array(
              'field' =&gt; 'email',
              'label' =&gt; 'Email Address',
              'rules' =&gt; 'required',
          ),
          array(
              'field' =&gt; 'area_code',
              'label' =&gt; 'Area Code',
              'rules' =&gt; 'required|is_numeric',
          ),
          array(
              'field' =&gt; 'number',
              'label' =&gt; 'Phone Number (without area code)',
              'rules' =&gt; 'required|is_numeric',
          ),
          array(
              'field' =&gt; 'extension',
              'label' =&gt; 'Extension (if applicable)',
              'rules' =&gt; '',
          ),
          array(
              'field' =&gt; 'type',
              'label' =&gt; 'Type',
              'rules' =&gt; '',
          ),
      ));
      $this-&gt;form_validation-&gt;set_error_delimiters('&lt;div class="alert alert-error"&gt;&lt;span style="color: red;"&gt;', '&lt;/span&gt;&lt;/div&gt;');
      if (!$this-&gt;form_validation-&gt;run()) {
          /**
           * Display Forms
           */
          $this-&gt;load-&gt;view('form_start');
          $this-&gt;load-&gt;view('client_form');
          $this-&gt;load-&gt;view('phone_form');
          $this-&gt;load-&gt;view('submit');
      } else {
          /**
           * Store data and Display Success
           */
          $this-&gt;load-&gt;model('Clients');
          $client = new Clients();
          $client-&gt;client_id = $this-&gt;input-&gt;post('client_id');
          $client-&gt;first_name = $this-&gt;input-&gt;post('first_name');
          $client-&gt;last_name = $this-&gt;input-&gt;post('last_name');
          $client-&gt;address = $this-&gt;input-&gt;post('address');
          $client-&gt;apt_or_suite_number = $this-&gt;input-&gt;post('apt_or_suite_number');
          $client-&gt;city = $this-&gt;input-&gt;post('city');
          $client-&gt;state = $this-&gt;input-&gt;post('state');
          $client-&gt;zip_code = $this-&gt;input-&gt;post('zip_code');
          $client-&gt;save();
          $this-&gt;load-&gt;view('form_success', array(
              'client' =&gt; $client,
          ));
      }
  }
  
  }
  </code></pre>
  
  <p>My views are the forms associated with them. If you need them I can add them.</p>
  
  <p>I have been following a tutorial on Lynda.com to get my own site designed, but the presenter moves far too fast for me to understand completely what is going on. I thought I had gotten the insert statements correct, but apparently not. All it does is keep the data in the local session from what I can best tell.</p>
  
  <p>UPDATED</p>
  
  <pre><code>    &lt;hr&gt;
  &lt;legend&gt;Basic Client Information&lt;/legend&gt;
  &lt;div&gt;
      &lt;label for="first_name"&gt;First Name&lt;/label&gt;
      &lt;?php echo form_error('first_name'); ?&gt;
      &lt;input type="text" name="first_name" value="&lt;?php echo set_value('first_name'); ?&gt;" placeholder="John" /&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;label for="last_name"&gt;Last Name&lt;/label&gt;
      &lt;?php echo form_error('last_name'); ?&gt;
      &lt;input type="text" name="last_name" value="&lt;?php echo set_value('last_name'); ?&gt;" placeholder="Doe" /&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;label for="address"&gt;Address&lt;/label&gt;
      &lt;?php echo form_error('address'); ?&gt;
      &lt;input type="text" name="address" value="&lt;?php echo set_value('addres'); ?&gt;" placeholder="555 Dream Street"/&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;label for="apt_or_suite_number"&gt;Apartment or Suite Number&lt;/label&gt;
      &lt;?php echo form_error('apt_or_suite_number'); ?&gt;
      &lt;input type="text" name="apt_or_suite_number" value="&lt;?php echo set_value('apt_or_suite_number'); ?&gt;" placeholder="555"/&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;label for="zip_code"&gt;Postal Zip Code&lt;/label&gt;
      &lt;?php echo form_error('zip_code'); ?&gt;
      &lt;input type="text" name="zip_code" value="&lt;?php echo set_value('zip_code'); ?&gt;" placeholder="85022"/&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;label for="city"&gt;City&lt;/label&gt;
      &lt;?php echo form_error('city'); ?&gt;
      &lt;input type="text" name="city" value="&lt;?php echo set_value('city'); ?&gt;" placeholder="Loony Town"/&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;label for="state"&gt;State Abbreviation&lt;/label&gt;
      &lt;?php echo form_error('state'); ?&gt;
      &lt;input type="text" name="state" value="&lt;?php echo set_value('state'); ?&gt;" placeholder="ID"/&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;label for="email"&gt;Email Address&lt;/label&gt;
      &lt;?php echo form_error('email'); ?&gt;
      &lt;input type="email" name="email" value="&lt;?php echo set_value('email'); ?&gt;" placeholder="myemail@fake.com"/&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>NOTE: That the form_start has the form tag in it with the action to post.</p>