我需要运行保存/更新查询,但只更新那些非空的字段。我的表单包含一个FILE字段,如果我没有上传任何文件,那么当执行save()时,即使先前的数据已经显示,该字段也会变为空白,那么如何保存/更新但只包含要更新内容的字段? / p>
提前干杯和谢谢
答案 0 :(得分:1)
您始终可以添加检查用户是否上传文件的条件。 如果没有,请在数据库中为该文件选择当前值,并使用旧值更新它。
例如:
$update['description'] = $_POST['description']; //just another field
if($_POST['file']['name'] == "") //check if there's a new file
$update['filename'] = $this->book->file_name; //current file name
else
$update['filename'] = $_POST['file']['name']; //new file name in case the user uploaded a new file
$this->book->update($update); //update no matter what
答案 1 :(得分:1)
在您的模型中,您需要先测试数据是否为空,在这种情况下取消设置。
if ($this->data['file'] == "") {
unset($this->data['file']);
}