无法检索要在数据库中更新的ID

时间:2015-05-20 07:27:29

标签: php twitter-bootstrap codeigniter

我无法识别错误

模型(这里我匹配要更新的ID)

function get_account_record($a_id)
{

    $this->db->where('a_id', $a_id);
    $this->db->from('account_info');
    $query = $this->db->get();

    return $query->result();
}

控制器(通过post接收所有内容,接受ID i-e a_id)

function update($a_id)
{
    $data['a_id'] = $a_id;
    $data['view'] = $this->sf_model->get_account_record($a_id);

    $this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
    $this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');

表单验证

if ($this->form_validation->run() == FALSE)
{
    $this->load->view('viewUpdate', $data);
}
else {
    $data = array(

        'a_name' => $this->input->post('a_name'),
        'a_website' => $this->input->post('a_web'),
        'a_billingStreet' => $this->input->post('a_billingStreet'),
        'a_mobile' => $this->input->post('a_mobile'),);

    $this->db->where('a_id', $a_id);
    $this->db->update('account_info', $data);   

显示成功消息

        $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Successfully Updated!</div>');
        //redirect('salesforce' . $a_id);
    }

}

查看(ID和名称)

<div class="form-group">
    <div class="row colbox">
        <div class="col-lg-4 col-sm-4">
            <label for="a_id" class="control-label">Account ID</label>
        </div>
        <div class="col-lg-8 col-sm-8">
            <input id="a_id" name="a_id" placeholder="" disabled="disabled" type="text" class="form-control"  value="<?php echo $a_id;?>" />
            <span class="text-danger"><?php echo form_error('a_id'); ?></span>
        </div>
    </div>
</div>
<div class="form-group">
    <div class="row colbox">
        <div class="col-lg-4 col-sm-4">
            <label for="a_name" class="control-label">Account Name</label>
        </div>
        <div class="col-lg-8 col-sm-8">
            <input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control"  value="<?php echo $view[0]->a_name; ?>" />
            <span class="text-danger"><?php echo form_error('a_name'); ?></span>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

如果您不想让用户进行编辑,则添加只读而不是已禁用的属性

<div class="col-lg-8 col-sm-8">
        <input id="a_id" name="a_id" placeholder="" readonly="readonly" type="text" class="form-control"  value="<?php echo $a_id;?>" />
        <span class="text-danger"><?php echo form_error('a_id'); ?></span>
    </div>

如果要禁用,请添加隐藏元素

<input id="a1_id" name="a1_id" type="hidden" value="<?php echo $a_id;?>" />

控制器功能

function update($a_id)
{
    //read the value using input library    
    $a_id  = $this->input->post('a_id');

    //$a1_id  = $this->input->post('a1_id'); your hidden element value can be get here.
    $data['a_id'] = $a_id;
    $data['view'] = $this->sf_model->get_account_record($a_id);

    $this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
    $this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
}

答案 1 :(得分:0)

  

我的问题在更新查询中编辑where子句后解决了

$this->db->where('a_id', $_POST['a_id']);