title: Insert selected dropdown value of one table into another table
body: <p>I have a CodeIgniter application, where, in the view page, I'm populating a dropdown from a table named <code>department</code>.</p>
  
  <p>Here's the structure of <code>department</code> table:</p>
  
  <pre><code>dept_id       int(11) PK
  dept_name     varchar(10)
  </code></pre>
  
  <p>I have the following values in <code>department</code> table:</p>
  
  <pre><code>   dept_id                dept_name
        1                      EEE 
        2                      CSE
        3                      ME       
        4                      CE
        5                     ARCH
  </code></pre>
  
  <p>The values of dept_name are populated in my dropdown.</p>
  
  <p>My view file is <code>student_view.php</code></p>
  
  <pre><code> &lt;!DOCTYPE html&gt;
   &lt;html&gt;
  
   &lt;head&gt;
   &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
   &lt;title&gt;My first site in CI&lt;/title&gt;
   &lt;/head&gt;
  
   &lt;body&gt;
   &lt;h2&gt;Student Information&lt;/h2&gt;
  
   &lt;form method="post" action="&lt;?php echo base_url();?&gt;index.php/student/insert_student_db"&gt;
   &lt;table width="800" border="0"&gt;
   &lt;th width="213" align="right" scope="row"&gt;Name:&lt;/th&gt;
   &lt;td width="161"&gt;&lt;input type="text" name="name" size="60" /&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
   &lt;th align="right" scope="row"&gt;Roll:&lt;/th&gt;
   &lt;td&gt;&lt;input type="text" name="roll" size="60" /&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
   &lt;th align="right" scope="row"&gt;Department:&lt;/th&gt;
   &lt;td&gt;
   &lt;select name="department"&gt;
   &lt;?php 
      $sql = mysql_query("SELECT dept_name FROM department");
      while ($row = mysql_fetch_array($sql)){
           echo "&lt;option value=\"department1\"&gt;" . $row['dept_name'] . "&lt;/option&gt;";
      }
   ?&gt;
   &lt;/select&gt;
   &lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
   &lt;th align="right" scope="row"&gt;Email:&lt;/th&gt;
   &lt;td&gt;&lt;input type="text" name="email" size="60" /&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
   &lt;th align="right" scope="row"&gt;Mobile:&lt;/th&gt;
   &lt;td&gt;&lt;input type="text" name="mobile" size="60" /&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
   &lt;th align="right" scope="row"&gt;&amp;nbsp;&lt;/th&gt;
   &lt;td&gt;&lt;input type="submit" name="submit" value="Send" /&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;/table&gt;
   &lt;/form&gt;
  
   &lt;table width="600" border="1" cellpadding="5"&gt;
   &lt;tr&gt;
   &lt;th scope="col"&gt;Name&lt;/th&gt;
   &lt;th scope="col"&gt;Roll&lt;/th&gt;
   &lt;th scope="col"&gt;Department&lt;/th&gt;
   &lt;th scope="col"&gt;Email&lt;/th&gt;
   &lt;th scope="col"&gt;Mobile&lt;/th&gt;
   &lt;/tr&gt;
  
   &lt;?php foreach ($student_list as $std_key){ ?&gt;
   &lt;tr&gt;
   &lt;td&gt;&lt;?php echo $std_key-&gt;name; ?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo $std_key-&gt;roll; ?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo $std_key-&gt;department; ?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo $std_key-&gt;email; ?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo $std_key-&gt;mobile; ?&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;?php }?&gt;
   &lt;/table&gt;
   &lt;/body&gt;
   &lt;/html&gt; 
  </code></pre>
  
  <p>My controller is <code>student.php</code> </p>
  
  <pre><code>  &lt;?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Student extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            #$this-&gt;load-&gt;helper('url');
            $this-&gt;load-&gt;model('student_model');
        }
  
        //Show all Students
        public function index()
        {
            $data['student_list'] = $this-&gt;student_model-&gt;get_all_students();
            $this-&gt;load-&gt;view('student_view', $data);
        }
  
        //Insert a student
        public function insert_student_db()
        {
            $udata['name'] = $this-&gt;input-&gt;post('name');
            $udata['roll'] = $this-&gt;input-&gt;post('roll');
            $udata['department'] = $this-&gt;input-&gt;post('department');
            $udata['email'] = $this-&gt;input-&gt;post('email');
            $udata['mobile'] = $this-&gt;input-&gt;post('mobile');
  
            $res = $this-&gt;student_model-&gt;insert_student($udata);
  
            if($res)
            {
                header('location:'.base_url()."index.php/student/".$this-&gt;index());
            }
         }
      }
   ?&gt;
  </code></pre>
  
  <p>My model is <code>student_model.php</code></p>
  
  <pre><code>&lt;?php
  class Student_model extends CI_Model 
  {
      function __construct()
      {
          parent::__construct();
          $this-&gt;load-&gt;database();
      }   
  
      //To retrieve all students
      public function get_all_students()
      {
          $query = $this-&gt;db-&gt;get('student');
          return $query-&gt;result();
      }
  
      //To add a new student to the database
      public function insert_student($data)
      {
          return $this-&gt;db-&gt;insert('student', $data);
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>What I want to do is: I want to insert the selected <code>dept_name</code> value in dropdown list of my view page, into the <code>department</code> column of another table named <code>student</code>, which is associated with my model <code>student_model.php</code>. But the value that is inserted is a string - 'department', instead of the dropdown value. What is wrong with my code?</p>
  
  <p>Here is the structure of my <code>student</code> table:</p>
  
  <pre><code>  name             varchar(30)
    roll             varchar(10)
    department       varchar(10)
    email            varchar(50)
    mobile           varchar(15)
  </code></pre>