将一个表的选定下拉值插入另一个表

时间:2014-09-25 13:02:04

标签: php html mysql codeigniter dropdownbox

我有一个CodeIgniter应用程序,在视图页面中,我正在填充名为department的表格中的下拉列表。

这是department表的结构:

dept_id       int(11) PK
dept_name     varchar(10)

我在department表中有以下值:

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

dept_name的值填充在我的下拉列表中。

我的观看文件为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> 

我的控制器是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());
          }
       }
    }
 ?>

我的模型是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);
    }

}
?>

我想要做的是:我想在我的视图页的下拉列表中将选定的dept_name值插入另一个名为department的表的student列中,这是与我的模型student_model.php相关联。但是插入的值是一个字符串 - 'department',而不是下拉值。我的代码出了什么问题?

以下是我的student表的结构:

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

1 个答案:

答案 0 :(得分:2)

如果你想要这个作业:

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

将部门名称dept_name放在字段$udata['department']中,然后您需要将<option>的值设置为dept_name为这样:

<?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>";
}
?>