2012-02-26 58 views
0

我需要運行保存/更新查詢,但只更新那些非空的字段。我的表單包含一個FILE字段,如果我沒有上傳任何文件,那麼當執行save()時,即使先前的數據被顯示,該字段仍然爲空,所以如何保存/更新但只更新內容的字段?如何更新,但如果字段爲空不是

歡呼和預先感謝

回答

1

在你的模型,你需要在之前的測試,如果數據是空的或不和取消設置它在這種情況下。

if ($this->data['file'] == "") { 
    unset($this->data['file']); 
} 
+0

得益於它完美的作品,但只有一件事是不是在我的模型,其中我需要檢查,這是在我的控制器:) – ReynierPM 2012-02-26 16:58:25

+0

取決於你的應用程序,但我會讓這個任務交給我的模型。無論如何,只要它有效......;) – Liyali 2012-02-26 17:05:36

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 
相關問題