【发布时间】:2015-07-15 05:47:02
【问题描述】:
我正在做一个学生管理系统,我想更新记录,所有代码都可以正常工作,但更新操作也没有完成,所有选择元素,如下拉菜单,单选按钮都没有检查为数据库中的数据。
控制器代码:
//code to edit student record.
public function edit($stu_id){
$this->load->model('model_editstudent');
$data['r']=$this->model_editstudent ->get_student_row($stu_id);
$this->load->view('view_editstudent', $data);
}
#update query.
function update(){
$stu_id=$this->input->post('stu_id');
$data=array(
'name'=>$this->input->post('name'),
'address'=>$this->input->post('address'),
'sex'=>$this->input->post('sex'),
'yop'=>$this->input->post('yop'),
'interest'=>$this->input->post('interest'));
$this->db->where('stu_id',$stu_id);
$this->db->update('stu_record',$data);
redirect('Student_Controller/');
}
模型代码:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_EditStudent extends CI_Model{
function get_student_row($stu_id){
$this->db->where('stu_id',$stu_id);
$query=$this->db->get('stu_record');
#will return only one record(row)
return $query->row();
}
}
查看代码:
<form method="POST" action='<?php echo site_url("student_controller/update"); ?>'>
<b>Student ID:</b><br>
<input type="text" Name="name" readonly value="<?php echo $r->stu_id ?>"><br><br>
<b>Name of the Student:</b><br>
<input type="text" Name="name" value="<?php echo $r->name ?>"><br><br>
<b>Address:</b><br>
<textarea name="address" rows="5" cols="22" ><?php echo $r->address ?></textarea><br><br>
<b>Gender:</b><br>
<input type="radio" name="sex" value="<?php if($r->sex=='Male') ?>">Male<br>
<input type="radio" name="sex" value="<?php if($r->sex=='Female') ?>">Female<br><br>
<b>Expected Year of Passing:</b><br>
<select name="yop" value="<?php echo $r->yop ?>">
<option value="0">Select Year</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select><br><br>
<b>Extra Curricular Interests:</b><br>
<input type="checkbox" name="interest" value="Sports"><label>Sports</label><br/>
<input type="checkbox" name="interest" value="Programming"><label>Programming</label><br/>
<input type="checkbox" name="interest" value="Arts"><label>Arts</label><br/>
<input type="checkbox" name="interest" value="Music"><label>Music</label><br/>
<br><input type="submit" value="Update">  
<a href='<?php echo site_url('student_controller/'); ?>'>Cancel</a>
</form>
【问题讨论】:
-
更新时遇到什么错误?
标签: php database codeigniter sql-update