2014-09-25 74 views
0

我有一個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值查看另一個表名爲studentdepartment列,該列與我的型號student_model.php相關聯。但是插入的值是一個字符串 - '部門',而不是下拉值。我的代碼有什麼問題?

這裏是我student表的結構:

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

回答

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

非常好!我得到了我的預期輸出,並且可以成功地在'department'中插入'dept_name'的值。 Muchos Gracias Senor Arbiza提供了這樣一個快速和準確的解決方案。 :) – 2014-09-25 13:17:06

+1

Ha sido un precer,estamos para servirle! :) – 2014-09-25 13:18:34

相關問題