我无法使用codeigniter更新数据库中的数据。这是我的控制者:
public function update_mission_vision($id)
{
$this->securePage();
$data['mission'] = $mission = $this->about_model->get_mission_vision($id);
if ($mission == false) {
$this->session->set_flashdata('msg', 'The mission does not exist.');
redirect('about/manage-miteri-mission-vision');
} else {
if ($this->input->post('submit')) {
$this->form_validation->set_rules('title', 'title', 'required|max_length[240]');
$this->form_validation->set_rules('details', 'details', 'required|max_length[240]');
if ($this->form_validation->run()) {
$added_on = time();
$this->about_model->update_mission_vision($added_on, $id);
$this->session->set_flashdata('msg', 'mission updated successfully.');
redirect('about/manage-miteri-mission-vision');
}
}
}
$data['title'] = "$mission->title | miteripkr.com";
$data['keywords'] = "$mission->title";
$data['content'] = $this->load->view('about-us/update_mission_vision', $data, true);
$this->load->view('miteri', $data);
}
这是我的模特:
function get_all_mission_vision($limit = null, $offset = null)
{
if ($limit)
{
$this->db->limit($limit. $offset);
}
return $this->db->get('mission_vision');
}
function get_mission_vision($id)
{
$this->db->where('id', $id);
$query = $this->db->get('mission_vision');
if ($query->num_rows()) {
return $query->row();
} else {
return false;
}
}
function update_mission_vision($id, $added_on)
{
$this->db->where('id', $id);
$this->db->set('title', $this->input->post('title'));
$this->db->set('details', $this->input->post('details'));
$this->db->set('added_on', $added_on);
$this->db->update('mission_vision');
}
这是我的观点:
<**div class="container-fluid" style="background:#fff; margin-top:10px; border-radius:5px; min-height:600px; margin-bottom:10px;">
<form action="" method="post" style="margin-top:30px;">
<?php echo $this->session->flashdata('msg');?>
<ol class="breadcrumb">
<li><a href="#">admin</a></li>
<li><a href="#">about us</a></li>
<li class="active"><a href="#">update mission & vision</a></li>
</ol>
<div class="form-group">
<label for="title"> Title for Post</label>
<input class="form-control" type="text" name="title" placeholder="Enter Title" value="<?php echo set_value('title', $mission->title)?>">
<?php echo form_error('title');?>
</div>
<div class="form-group">
<label for="title"> Details for Post</label>
<textarea class="ckeditor" name="details" placeholder="Enter Details" style="height:200px;"><?php echo set_value('details', $mission->details); ?></textarea>
<?php echo form_error('details');?>
</div>
<button class="btn btn-success" name="submit" type="submit" value="update"><span class="glyphicon glyphicon-ok"> update</span></button>
</form>
</div>
它甚至会打印成功消息,但我的数据没有更新,为什么?
答案 0 :(得分:0)
也许这是函数update_mission_vision
的调用。您切换了$id
和$added_on
。
当然,就像@Kamran Adil所说的那样,你必须将后期数据提供给你的模型。
更新: 这是一个如何在模型中编写更新函数的示例。所以你可以查看并制作自己的更新功能。
/**
* @name string TABLE_NAME Holds the name of the table in use by this model
*/
const TABLE_NAME = 'table';
/**
* @name string PRI_INDEX Holds the name of the tables' primary index used in this model
*/
const PRI_INDEX = 'field';
/**
* Updates selected record in the database
*
* @param Array $data Associative array field_name=>value to be updated
* @param Array $where Optional. Associative array field_name=>value, for where condition. If specified, $id is not used
* @return int Number of affected rows by the update query
*/
public function update(Array $data, $where = array()) {
if (!is_array($where)) {
$where = array(self::PRI_INDEX => $where);
}
$this->db->update(self::TABLE_NAME, $data, $where);
return $this->db->affected_rows();
}
答案 1 :(得分:0)
也许你丢失了网址中的$ id。
在视图中使用$ id放置一个隐藏的输入,然后在控制器中,将此$id = $this->input->post('id');
设置为获取$ id。并在控制器update_mission_vision() {