Search code examples
phphtmlmysqlcodeigniterdropdownbox

Insert selected dropdown value of one table into another table


I have a CodeIgniter application, where, in the view page, I'm populating a dropdown from a table named department.

Here's the structure of department table:

dept_id       int(11) PK
dept_name     varchar(10)

I have the following values in department table:

   dept_id                dept_name
      1                      EEE 
      2                      CSE
      3                      ME       
      4                      CE
      5                     ARCH

The values of dept_name are populated in my dropdown.

My view file is student_view.php

 <!DOCTYPE html>
 <html>

 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>My first site in CI</title>
 </head>

 <body>
 <h2>Student Information</h2>

 <form method="post" action="<?php echo base_url();?>index.php/student/insert_student_db">
 <table width="800" border="0">
 <th width="213" align="right" scope="row">Name:</th>
 <td width="161"><input type="text" name="name" size="60" /></td>
 </tr>
 <tr>
 <th align="right" scope="row">Roll:</th>
 <td><input type="text" name="roll" size="60" /></td>
 </tr>
 <tr>
 <th align="right" scope="row">Department:</th>
 <td>
 <select name="department">
 <?php 
    $sql = mysql_query("SELECT dept_name FROM department");
    while ($row = mysql_fetch_array($sql)){
         echo "<option value=\"department1\">" . $row['dept_name'] . "</option>";
    }
 ?>
 </select>
 </td>
 </tr>
 <tr>
 <th align="right" scope="row">Email:</th>
 <td><input type="text" name="email" size="60" /></td>
 </tr>
 <tr>
 <th align="right" scope="row">Mobile:</th>
 <td><input type="text" name="mobile" size="60" /></td>
 </tr>
 <tr>
 <th align="right" scope="row">&nbsp;</th>
 <td><input type="submit" name="submit" value="Send" /></td>
 </tr>
 </table>
 </form>

 <table width="600" border="1" cellpadding="5">
 <tr>
 <th scope="col">Name</th>
 <th scope="col">Roll</th>
 <th scope="col">Department</th>
 <th scope="col">Email</th>
 <th scope="col">Mobile</th>
 </tr>

 <?php foreach ($student_list as $std_key){ ?>
 <tr>
 <td><?php echo $std_key->name; ?></td>
 <td><?php echo $std_key->roll; ?></td>
 <td><?php echo $std_key->department; ?></td>
 <td><?php echo $std_key->email; ?></td>
 <td><?php echo $std_key->mobile; ?></td>
 </tr>
 <?php }?>
 </table>
 </body>
 </html> 

My controller is student.php

  <?php
  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Student extends CI_Controller
  {
      function __construct()
      {
          parent::__construct();
          #$this->load->helper('url');
          $this->load->model('student_model');
      }

      //Show all Students
      public function index()
      {
          $data['student_list'] = $this->student_model->get_all_students();
          $this->load->view('student_view', $data);
      }

      //Insert a student
      public function insert_student_db()
      {
          $udata['name'] = $this->input->post('name');
          $udata['roll'] = $this->input->post('roll');
          $udata['department'] = $this->input->post('department');
          $udata['email'] = $this->input->post('email');
          $udata['mobile'] = $this->input->post('mobile');

          $res = $this->student_model->insert_student($udata);

          if($res)
          {
              header('location:'.base_url()."index.php/student/".$this->index());
          }
       }
    }
 ?>

My model is student_model.php

<?php
class Student_model extends CI_Model 
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }   

    //To retrieve all students
    public function get_all_students()
    {
        $query = $this->db->get('student');
        return $query->result();
    }

    //To add a new student to the database
    public function insert_student($data)
    {
        return $this->db->insert('student', $data);
    }

}
?>

What I want to do is: I want to insert the selected dept_name value in dropdown list of my view page, into the department column of another table named student, which is associated with my model student_model.php. But the value that is inserted is a string - 'department', instead of the dropdown value. What is wrong with my code?

Here is the structure of my student table:

  name             varchar(30)
  roll             varchar(10)
  department       varchar(10)
  email            varchar(50)
  mobile           varchar(15)

Solution

  • If you want this assignment:

    $udata['department'] = $this->input->post('department');
    

    to put the department's name dept_name in the field $udata['department'], then you need to set the <option>'s value to dept_name as this:

    <?php 
    $sql = mysql_query("SELECT dept_name FROM department");
    while ($row = mysql_fetch_array($sql)){
        echo "<option value=\"{$row['dept_name']}\">" . $row['dept_name'] . "</option>";
    }
    ?>