【发布时间】: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