title: CodeIgniter - Enter data into textbox and check for matching record from database on submit
body: <p>I have a CodeIgniter application where I need to add some information in a form in my view page named <code>meal_add.php</code>. Here is the form:</p>
  
  <pre><code> &lt;div id="content" class="box"&gt;
                  &lt;form action="&lt;?php echo base_url(); ?&gt;index.php/admin_logins/meal2" method="post"&gt;
                      &lt;fieldset&gt;
                          &lt;legend id="add_employee_legend"&gt;
                              Add Meal Information
                          &lt;/legend&gt;
                          &lt;div&gt;
                              &lt;label id= "emp_id_add_label"&gt;Employee ID:&lt;/label&gt;
                              &lt;input type="text" name="emp_id" id = "employee_id_add" placeholder="Employee ID" required="1"/&gt;
                          &lt;/div&gt;
                          &lt;br&gt;
                          &lt;div&gt;
                              &lt;label id= "is_guest_add_label"&gt;Guests?&lt;/label&gt;
                              &lt;input type="checkbox" name ="is_guest_checkbox" class ="guestcheck" value="1" onchange="valueChanged()"/&gt;
                          &lt;/div&gt;
                          &lt;br&gt;
                          &lt;div id = "guestnum" hidden = "true"&gt;
                              &lt;label id= "num_of_guest_add_label"&gt;No. of Guests:&lt;/label&gt;
                              &lt;input type="text" name="num_of_guest" id = "num_of_guest_add" placeholder='0'/&gt;
                          &lt;/div&gt;
                          &lt;div&gt;
                              &lt;label id= "remarks_add_label"&gt;Remarks:&lt;/label&gt;
                              &lt;textarea rows="1" cols="20" style="margin-left: 35px"&gt;&lt;/textarea&gt;
                          &lt;/div&gt;
                          &lt;input type="submit" name="submit" id = "meal_info_submit" value="Save Meal Information"/&gt;
                          &lt;button id = "cancel_button" onclick="location.href='&lt;?php echo base_url(); ?&gt;index.php/admin_logins/meal'"&gt;
                              Cancel
                          &lt;/button&gt;
                      &lt;/fieldset&gt;
                  &lt;/form&gt;
  </code></pre>
  
  <p>My controller method for add -</p>
  
  <pre><code>function meal2()
  {
      if($_POST)
      {
          date_default_timezone_set('Asia/Dacca');
          $mdata['emp_id'] = $this-&gt;input-&gt;post('emp_id');
          $mdata['entry_date'] = date('Y-m-d');
          $mdata['is_guest'] = $this-&gt;input-&gt;post('is_guest_checkbox');
          $mdata['num_of_guest'] = $this-&gt;input-&gt;post('num_of_guest');
  
          $mdata['remarks'] = $this-&gt;input-&gt;post('remarks');
          $res = $this-&gt;meal_model-&gt;insert_meal($mdata);
          if($res)
          {   
              $this-&gt;session-&gt;set_flashdata('message','Meal information added successfully');
              redirect("admin_logins/meal");
          }
      }
      else
      {
          $this-&gt;load-&gt;view('admin_logins/meal_add');
      }
  
  }
  </code></pre>
  
  <p>And my model method - </p>
  
  <pre><code>public function insert_meal($data)
  {
      return $this-&gt;db-&gt;insert('meal', $data);    
  }
  </code></pre>
  
  <p>I have a table named <code>employee</code>, the structure is as follows:</p>
  
  <pre><code>    column              data type
        id                 int PK
      emp_id             varchar(15)
     emp_name            varchar(50)
   emp_mobile_no         varchar(15)
  </code></pre>
  
  <p>I have another table named <code>meal</code>, structure is as follows:</p>
  
  <pre><code>    column              data type
        id                 int PK
      emp_id             varchar(15)
     entry_date             date
     is_guest               int
   num_of_guest             int
     remarks                text
  </code></pre>
  
  <p>What I need to do is: whenever I'll enter an ID in Employee ID textbox in my form <code>meal_add.php</code> page, system will determine whether my entered id text matches with value of <code>employee.emp_id</code>. If a match is found, then data will be successfully saved in the <code>meal</code> table as <code>meal.emp_id</code>, and the value that will be saved is the corresponding <code>employee.id</code>(the value in the primary key column of <code>employee</code> table). If no match is found, a php flash message will be displayed that says ID doesn't exist on top of my <code>meal_add.php</code> view page. I know that a JOIN query is needed, but the text matching part seems very difficult to me and I can't figure out how to organize my code and where to put them. Please help me out.</p>
  
  <p>My Database is MySQL.</p>
  
  <p><strong>EDIT - 1:</strong></p>
  
  <p>I've done this in my controller method - </p>
  
  <pre><code>    $temp = $this-&gt;input-&gt;post('emp_id');
      $sql = "SELECT e.id FROM employee AS e WHERE e.emp_id = ?";
      $mdata['emp_id'] = $this-&gt;db-&gt;query($sql, $temp)-&gt;emp_id;
  
  
      if($mdata['emp_id'] == '')
      {
          $this-&gt;session-&gt;set_flashdata('message','Employee ID does\'nt exist');
          redirect("admin_logins/meal");
      }
  </code></pre>
  
  <p>No matter what value I'm providing in <code>$temp</code>, I always get the flash message and get redirected. </p>
  
  <p>I've done this as well-</p>
  
  <pre><code>    echo "&lt;pre&gt;";
      print_r($mdata);
      die();
  </code></pre>
  
  <p>And after doing <code>echo</code> and <code>print_r()</code>, I got this - </p>
  
  <pre><code>  A PHP Error was encountered
  
    Severity: Notice
  
    Message: Undefined property: CI_DB_mysql_result::$emp_id
  
    Filename: controllers/admin_logins.php
  
    Line Number: 150
  
    Array
    (
        [emp_id] =&gt; 
    )
  </code></pre>