PHP CodeIgniter:我删除数据库数据的代码工作不正常,你能发现问题吗?

PHP CodeIgniter:我删除数据库数据的代码工作不正常,你能发现问题吗?,php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,基本上,当用户按下delete按钮时,我试图删除数据库数据,其工作方式是我将数据放在表中,我通过数组收集数据数组中的数组表示每条记录,将其传递到视图中,显示每条记录中的一部分,用户决定是否删除它,如果他们按delete键,那么字段数据将被发布回我的控制器,并传递到一个模型中,在该模型中,它运行一个查询,根据它拥有的UID删除数据,这应该可以工作,但实际发生的是,一切正常,我没有收到任何错误,但数据没有被删除,我怀疑这是因为表单没有正确地发回数据,你们中的一些深度CI用户知道为什么吗?以下是相关代

基本上,当用户按下delete按钮时,我试图删除数据库数据,其工作方式是我将数据放在表中,我通过数组收集数据数组中的数组表示每条记录,将其传递到视图中,显示每条记录中的一部分,用户决定是否删除它,如果他们按delete键,那么字段数据将被发布回我的控制器,并传递到一个模型中,在该模型中,它运行一个查询,根据它拥有的UID删除数据,这应该可以工作,但实际发生的是,一切正常,我没有收到任何错误,但数据没有被删除,我怀疑这是因为表单没有正确地发回数据,你们中的一些深度CI用户知道为什么吗?以下是相关代码:

控制器:

public function clist() { //function to show the client list and edit/delete buttons
        $this->load->model('list_model');
        $fields = $this->list_model->listcliname();
        $data = array();
        $data['fields'] = $fields;
        $this->load->view('clientlist', $data);
        if ($_POST['del'] === 1) { //if the user selects the delete button then call model to delete the fields for the button they selected
        $fields = $_POST['fields'];
        $this->list_model->delcli($fields);
        }
public function clist() { //function to show the client list and edit/delete buttons
        $this->load->model('list_model');
        $fields = $this->list_model->listcliname();
        $data = array();
        $data['fields'] = $fields;
        $this->load->view('clientlist', $data);
        if ($_POST['del'] == 1) { //if the user selects the delete button then call model to delete the fields for the button they selected
        $fields = $_POST['fields'];
        $this->list_model->delcli($fields);
        }
型号:

class list_model extends CI_Model {

public function listcliname()
    {
        return $this->db->get('clients')->result_array(); //get an array of arrays (one array for each record in table)
    }
public function delcli($fields)
    {
        $this->db->where('UID', $fields['UID']);
        $this->db->delete('clients', $fields);
    }

}
    public function delcli($fields)
    {
        $this->db->where('UID', $fields);
        $this->db->delete('clients');
    }
}
视图:

您可能可以使用ClisteEdit表单操作忽略正常工作的表单

感谢您的帮助,谢谢


编辑:可能是因为我发回的是一个数组?我可以拆分数组并添加多个隐藏提交以提交每个值,然后将它们再次添加到控制器中的数组中,但我希望避免这种情况。

我认为您没有打印下一行中字段的值

<input type="hidden" name="fields" value="<?php $field ?>" />
换成

<input type="hidden" name="fields" value="<?php echo $field ?>" />

您可以检查元素以检查隐藏字段的值是否正确。

所以基本上是因为我试图发回一个数组!还有一些其他错误,我将发布修改后的代码,但我的印象是,我需要所有数据来实际删除记录,但因为查询类似于从表中删除,其中UID=someint,所以我实际上只需要发回UID,所以我从字段数组中回显UID索引,并修复了其他错误而且它有效

控制器:

public function clist() { //function to show the client list and edit/delete buttons
        $this->load->model('list_model');
        $fields = $this->list_model->listcliname();
        $data = array();
        $data['fields'] = $fields;
        $this->load->view('clientlist', $data);
        if ($_POST['del'] === 1) { //if the user selects the delete button then call model to delete the fields for the button they selected
        $fields = $_POST['fields'];
        $this->list_model->delcli($fields);
        }
public function clist() { //function to show the client list and edit/delete buttons
        $this->load->model('list_model');
        $fields = $this->list_model->listcliname();
        $data = array();
        $data['fields'] = $fields;
        $this->load->view('clientlist', $data);
        if ($_POST['del'] == 1) { //if the user selects the delete button then call model to delete the fields for the button they selected
        $fields = $_POST['fields'];
        $this->list_model->delcli($fields);
        }
型号:

class list_model extends CI_Model {

public function listcliname()
    {
        return $this->db->get('clients')->result_array(); //get an array of arrays (one array for each record in table)
    }
public function delcli($fields)
    {
        $this->db->where('UID', $fields['UID']);
        $this->db->delete('clients', $fields);
    }

}
    public function delcli($fields)
    {
        $this->db->where('UID', $fields);
        $this->db->delete('clients');
    }
}
视图:


您在哪里定义了表单的操作?var\u dump$\u POST['del']==1的结果是什么?如果为false,则在If语句中put$_POST[del]==1is$fields['UID']数组处于何处条件?好的,我这样做了,现在我收到了错误消息:非法字符串偏移量'UID'文件名:models/list_model.php行号:15和:Message:Uninitialized string offset:0文件名:models/list_model.php行号:15我尝试将其更改回去,但仍然收到这些错误,我不确定它们是什么告诉meI不能这样做,因为$field是一个数组如果field是一个数组,那么你可以使用echo内爆“,”,$field;然后在模型文件中,您必须使用$this->db->where_在'UID'中,$fields['UID'];但请预先处理$fields['UID'],以防止sql注入