Php CodeIgniter-按ID更新单个记录-失败

Php CodeIgniter-按ID更新单个记录-失败,php,mysql,codeigniter,mysqli,Php,Mysql,Codeigniter,Mysqli,我是CodeIgniter的新用户,看过其他帖子,遗憾的是给出的答案并不能解决这个问题 我使用一个表单来创建记录(也上传图像文件,并在db中创建指向图像的链接作为字段),这一切都可以正常工作。我现在正试图编辑一个特定的记录。我有一个表单视图,它可以工作,并且是从数据库中使用$id=$this->uri->segment(3)预先填充的,以获取正确的记录-这一切都可以工作 失败的地方是更新记录 我的控制器 function edit_stock_vehicle_record() {

我是
CodeIgniter
的新用户,看过其他帖子,遗憾的是给出的答案并不能解决这个问题

我使用一个表单来创建记录(也上传图像文件,并在db中创建指向图像的链接作为字段),这一切都可以正常工作。我现在正试图编辑一个特定的记录。我有一个表单视图,它可以工作,并且是从数据库中使用
$id=$this->uri->segment(3)
预先填充的,以获取正确的记录-这一切都可以工作

失败的地方是更新记录

我的控制器

    function edit_stock_vehicle_record()
    {
        $this->load->library('form_validation');


        // field name, error message, validation rules
        $this->form_validation->set_rules('title', 'Title', 'trim|required');
        $this->form_validation->set_rules('make', 'Make', 'trim|required');
        $this->form_validation->set_rules('model', 'Model', 'trim|required');
        $this->form_validation->set_rules('fuel', 'Fuel Type', 'trim|required');
        $this->form_validation->set_rules('enginesize', 'Engine Size', 'trim|required');
        $this->form_validation->set_rules('trans', 'Transmission', 'trim|required');
        $this->form_validation->set_rules('reg_no', 'Registration Number', 'trim|required');
        $this->form_validation->set_rules('year', 'Year', 'trim|required');
        $this->form_validation->set_rules('colour', 'Colour', 'trim|required');
        $this->form_validation->set_rules('mileage', 'Mileage', 'trim|required');
        $this->form_validation->set_rules('long_desc', 'Description', 'trim|required');
        $this->form_validation->set_rules('purchase_price', 'Purchase Price', 'trim|required');
        $this->form_validation->set_rules('sale_price', 'Sale Price', 'trim|required');


        if($this->form_validation->run() == FALSE)
        {
            $data['main_content'] = 'vehicle_display2';
            $this->load->view('includes/template', $data);
        }

        else
        {           
            $this->load->model('manage_model');
            if($query = $this->manage_model->edit_stock_vehicle_record())
            {
                $data['main_content'] = 'vehicle_added';
                $this->load->view('includes/template', $data);
            }
            else
            {

                $data['main_content'] = 'add_vehicle4';
                $this->load->view('includes/template', $data);      
            }
        }


    }
我的模特

    function edit_stock_vehicle_record()
    {
        $this->load->helper('array');
        $this->load->helper('html');
        $id = $this->uri->segment(3);
        $config['upload_path'] = 'c:/wamp/www/honest/images';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;

        $this->load->library('upload', $config);    

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $data['main_content'] = 'imageupload';
            $this->load->view('includes/template', $data);
            echo "FAILED";
        }
        else
        {

        $image_data = $this->upload->data();

        $vehicle_update_data = array(
            'title' => $this->input->post('title'),
            'make' => $this->input->post('make'),
            'model' => $this->input->post('model'),
            'fuel' => $this->input->post('fuel'),
            'enginesize' => $this->input->post('enginesize'),
            'trans' => $this->input->post('trans'),
            'reg_no' => $this->input->post('reg_no'),   
            'year' => $this->input->post('year'),
            'colour' => $this->input->post('colour'),
            'mileage' => $this->input->post('mileage'),
            'long_desc' => $this->input->post('long_desc'),
            'purchase_price' => $this->input->post('purchase_price'),           
            'sale_price' => $this->input->post('sale_price'),
            'image' => $image_data['file_name']

        );
        $this->load->helper('url');
        $id = $this->uri->segment(3);
        $update = $this->db->update('stock_vehicles', $vehicle_update_data, array('id' => $id));
        return $update;
        }
    }
我也尝试过使用….的版本

    $this->db->where('id', $id);
    $this->db->update('stock_vehicles', $vehicle_update_data);
但这也失败了,似乎没有正确识别
$id

如果我删除上面的内容,只需使用
$this->db->update('stock\u vehicles',$vehicle\u update\u data)它确实会更新数据库中的每一条记录(doh!)幸运的是,当我发现这一点时,数据库中没有“真实”的数据

不确定为什么它没有拾取
$id
-所有输入都已收到

干杯


DP

首先,必须在控制器中加载url帮助程序

$this->load->helper('url');
然后您应该尝试以下代码:

$id = $this->uri->segment(3);
$vehicle_update_data =array(
  'title' => $this->input->post('title'),
  'make' => $this->input->post('make'),
  'model' => $this->input->post('model'),
  'fuel' => $this->input->post('fuel'),
  'enginesize' => $this->input->post('enginesize'),
  'trans' => $this->input->post('trans'),
  'reg_no' => $this->input->post('reg_no'),   
  'year' => $this->input->post('year'),
  'colour' => $this->input->post('colour'),
  'mileage' => $this->input->post('mileage'),
  'long_desc' => $this->input->post('long_desc'),
  'purchase_price' => $this->input->post('purchase_price'),           
  'sale_price' => $this->input->post('sale_price'),
  'image' => $image_data['file_name']
);
$this->db->WHERE('id',$id)->update('your_table',$vehicle_update_data);
return true;

首先,必须在控制器中加载url帮助程序:

$this->load->helper('url');
然后您应该尝试以下代码:

$id = $this->uri->segment(3);
$vehicle_update_data =array(
  'title' => $this->input->post('title'),
  'make' => $this->input->post('make'),
  'model' => $this->input->post('model'),
  'fuel' => $this->input->post('fuel'),
  'enginesize' => $this->input->post('enginesize'),
  'trans' => $this->input->post('trans'),
  'reg_no' => $this->input->post('reg_no'),   
  'year' => $this->input->post('year'),
  'colour' => $this->input->post('colour'),
  'mileage' => $this->input->post('mileage'),
  'long_desc' => $this->input->post('long_desc'),
  'purchase_price' => $this->input->post('purchase_price'),           
  'sale_price' => $this->input->post('sale_price'),
  'image' => $image_data['file_name']
);
$this->db->WHERE('id',$id)->update('your_table',$vehicle_update_data);
return true;
我对表格有一个看法,这是有效的,并且是从 db使用$id=$this->uri->segment(3)获取正确的记录- 这一切都有效

不要对表单更新执行此操作。只需将id包含在隐藏的表单字段中。更容易、更安全等

echo form_hidden( 'id', $id  );
我对表格有一个看法,这是有效的,并且是从 db使用$id=$this->uri->segment(3)获取正确的记录- 这一切都有效

不要对表单更新执行此操作。只需将id包含在隐藏的表单字段中。更容易、更安全等

echo form_hidden( 'id', $id  );

你可以粘贴你的URL试试这个$id=$this->uri->segment(3,0);首先检查是否有任何东西进入id;url为-并且正确生成了最后一段。我在视图中使用回音,并且它显示正确。have u echo ur variable id是任何传入的id。你可以粘贴你的URLTY this$id=$this->uri->segment(3,0);首先检查是否有任何东西进入id;url为-并且正确生成了最后一段。我在视图中使用,它显示正确。我尝试过:$this->db->WHERE('id',$id)->update('your_table',$vehicle_update_data));但是得到同样的东西。我还尝试调用$id=$this->uri->segment(3);在控制器中,以这种方式将id传递给模型,但仍然没有乐趣。我还尝试使用已知记录进行测试:$this->db->WHERE('id',63)->update('your_table',$vehicle_update_data);这个很好用,所以我知道它找不到$id——但如果我能明白为什么,我会被诅咒的!我还尝试了$id=$this->uri->segment(3,0);有和没有附加零-相同的结果:(然后尝试从html传递id,从控制器函数接收id,并在idI尝试:$this->db->WHERE('id',$id)->update('your_table',$vehicle_update_data)之前将其传递到html中的modelin,就像这样添加base_url(),但得到的结果是一样的。我还尝试调用$id=$this->uri->segment(3);在控制器中,以这种方式将id传递给模型,但仍然没有乐趣。我还尝试使用已知记录进行测试:$this->db->WHERE('id',63)->update('your_table',$vehicle_update_data);这很好,所以我知道它找不到$id-但如果我能找到原因,我会被诅咒的!我还尝试了$id=$this->uri->segment(3,0);有和没有额外的零-相同的结果。:(然后尝试从html传递id,从控制器函数接收id,并将其传递到html中的模型,如下所示:在idPERFECT之前添加base_url()。-谢谢加载。完美!-谢谢加载。