【问题标题】:PHP CodeIgniter: My code to delete database data is not working correctly, can you spot the issue?PHP CodeIgniter:我删除数据库数据的代码不能正常工作,你能发现问题吗?
【发布时间】:2014-09-25 09:22:00
【问题描述】:

基本上我试图在用户按下删除按钮时删除数据库数据,其工作方式是我在表中有数据,我通过数组数组收集它(数组内的数组代表每条记录) ,将其传递到视图中,显示每条记录(其中一些),用户决定是否删除它,如果他们按下删除,则字段数据将被发送回我的控制器并传递到运行查询以删除的模型中基于它拥有的 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);
        }

型号:

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);
    }

}

查看:

<html>
    <body>

            <?php
            foreach($fields as $field) {
                 ?>
                 <ul>
                 <li>
                 <?php echo $field['name']; ?>
                 </li> 
                 <li>
                 <?php echo $field['contact']; ?>
                 </li>
                 <li>
                <form action="clistedit" method="post">
                <button name="UID" type="submit" value="<?php echo $field['UID']?>">edit</button>
                <input type="hidden" name="sub" value="1" />
                </form>
                </li>
                <li>
                <form action="clist" method="post"> 
                <input type="hidden" name="fields" value="<?php $field ?>" />
                <button name="del" type="submit" value="1">delete</button>
                </form>
                </li>
                </ul>

                 <?php
            }

            ?>

(您可能可以使用 clistedit 表单操作忽略表单,它可以正常工作)

感谢任何帮助,谢谢!

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

【问题讨论】:

  • 你在哪里定义了表单的动作?
  • var_dump($_POST['del'] === 1) 的结果是什么?如果为假,则在 if 语句中放入 $_POST["del"]=="1"
  • $fields['UID'] 数组在哪里条件?
  • 好的,我这样做了,现在我收到错误“消息:非法字符串偏移 'UID' 文件名:models/list_model.php 行号:15”和:“消息:未初始化的字符串偏移: 0 文件名:models/list_model.php 行号:15" 我尝试将其改回,但我仍然收到这些错误,我不确定他们告诉我什么

标签: php mysql arrays codeigniter


【解决方案1】:

我认为您错过了在下一行中打印字段的值

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

改成

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

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

【讨论】:

  • 我不能这样做,因为 $field 是一个数组
  • 如果字段是一个数组,那么你可以使用 echo implode(',', $field);然后在模型文件中你必须使用 $this->db->where_in('UID', $fields['UID']);但请预处理 $fields['UID'] 以防止 sql 注入
【解决方案2】:

所以基本上这是我试图回发一个数组的事实!还有一些其他错误(我将发布修改后的代码)但我的印象是我需要所有数据才能实际删除记录,但因为查询类似于“DELETE FROM table WHERE 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 delcli($fields)
    {
        $this->db->where('UID', $fields);
        $this->db->delete('clients');
    }
}

查看:

<html>
    <body>

            <?php
            foreach($fields as $field) {
                 ?>
                 <ul>
                 <li>
                 <?php echo $field['name']; ?>
                 </li> 
                 <li>
                 <?php echo $field['contact']; ?>
                 </li>
                 <li>
                <form action="clistedit" method="post">
                <button name="UID" type="submit" value="<?php echo $field['UID']?>">edit</button>
                <input type="hidden" name="sub" value="1" />
                </form>
                </li>
                <li>
                <form action="clist" method="post"> 
                <input type="hidden" name="fields" value="<?php echo $field['UID']?>" />
                <button name="del" type="submit" value="1">delete</button>
                </form>
                </li>
                </ul>

                 <?php
            }

            ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多