【问题标题】:Codeigniter update values in a tableCodeigniter 更新表中的值
【发布时间】:2020-03-27 10:57:52
【问题描述】:

我正在学习将 codeigniter 与 rest_controller 库一起使用。

我正在搜索更新我的数据库中表中的一些值。

我的控制器中有:

public function guest_patch(){
        $idperson = $this->uri->segment(3);
        $person = array(
            'fiscalcode' => $this->input->post('fiscalcode'),
            'name' => $this->input->post('name'),
            'surname' => $this->input->post('surname')
        );
        $this->load->model('Person_model');
        $data = $this->Person_model->patch($idperson, $person);
        $this->response($data);
    }

在我的模型中:

public function patch($idperson, $person){
        $this->fiscalcode = $person['fiscalcode'];
        $this->name = $person['name'];
        $this->surname = $person['surname'];
        $result = $this->db->update('Persons', $this, array('id' => $idperson));
        if($result){
            return("Dati aggiornati");
        }
    }

我有两个问题要问你:

1) 在这种情况下,当我启动 update 方法时,我的数据库中的值将被删除(或用 0 替换)。事实上,我试图在我的模型中打印 $person 并且我传递的所有值(使用邮递员)都是空的。而如果我在控制器中手动插入数据,例如:

'fiscalcode' =>'NewFiscalCode'

db 中的数据被更新。 如何更新从表单数据中恢复的值(在我使用邮递员传递的那一刻)?

2) 如果我不想更新一个值,所以我将一个字段留空(例如,我在表单数据中只写了姓名和姓氏,但我不想更改财务代码字段),数据库中有关此字段“财政代码”的数据将被消除?

谢谢

【问题讨论】:

    标签: php rest codeigniter


    【解决方案1】:

    案例 1:

    我猜您在 DB 中将列 'fiscalcode' 指定为整数,而您正在尝试更新非 int 数据。所以 MySQL 将字段重置为 0;

    案例 2:

    更新 Controller 代码如下:

    $person = array(            
        'name' => $this->input->post('name'),
        'surname' => $this->input->post('surname')
    );
    
    if($this->input->post('fiscalcode')){
       $person['fiscalcode'] = $this->input->post('fiscalcode');
    }
    

    像这样更新模型代码:

    if(isset($person['fiscalcode'])){
        $this->fiscalcode = $person['fiscalcode'];
    }
    

    仅当您使用参数'fiscalcode'发布数据时才会分配

    【讨论】:

    • 这些值都不是整数。我认为问题出在控制器上。我试图 var_dump 对象 $person ,结果是:array (size=3) 'fiscalcode' => null 'name' => null 'surname' => null
    • @Jack 我想说您的表列可能被定义为“整数非空列”......所以如果您尝试插入一个非整数值,如“空”,它将重置其值为 0。
    • 好的,但我正在尝试使用邮递员(表单数据)插入一些值。例如,我尝试仅传递名称:NewName 和姓氏:NewSurname。但是帖子中的 var_dump 给我这些值的 null
    【解决方案2】:
    //use route for this in inside config/routes.php
    $route['edit/(:any)'] = 'controller_name/guest_patch/$1';
    
    public function guest_patch($idperson){
      $person = array(
                'fiscalcode' => $this->input->post('fiscalcode'),
                'name' => $this->input->post('name'),
                'surname' => $this->input->post('surname')
         );
      $this->load->model('Person_model');
      $data = $this->Person_model->patch($idperson, $person);
      $this->response($data);
        }
    //model
    public function patch($idperson, $person){
            return $this->db->where('idperson',$idperson)->update($person);
        }
    

    【讨论】:

    • 我已经尝试了你的建议,但结果没有改变。我认为问题出在控制器上。我试图 var_dump 对象 $person ,结果是:array (size=3) 'fiscalcode' => null 'name' => null 'surname' => null
    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2011-12-30
    • 2013-06-17
    相关资源
    最近更新 更多