[英]validating if imagefile is empty also have a separate query for a file that is not empty
我想有一個單獨的查詢,當圖像文件保留為空白時,僅更新圖像名稱和圖像文本;當用戶選擇新圖像時,更新所有圖像名稱,圖像文本和圖像本身。這是我的查詢:
if (isset($_POST['update'])) {
$update_id = $_POST["id"];
$update_imagename = $_POST['imagename'];
// Get image name
$update_imagefile = $_FILES['imagefile']['name'];
// Get text
$update_imagetext = mysqli_real_escape_string($db, $_POST['imagetext']);
// image file directory
$target = "images/portfolio/" . basename($update_imagefile);
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
$run = mysqli_query($db, $update_query);
if ($run) {
//javascript function to open in the same window
echo "<script>window.open('artworksupdate.php?=image has been updated','_self')</script>";
}
要確定您是否具有uploadFile,請在執行示例后執行!empty($_POST['imagefile'])
。
if (!empty($_POST)) {
$update_id = $_POST["id"];
// Get image name
$update_imagename = $_POST['imagename'];
// Get image description
$update_imagetext = $_POST['imagetext'];
// Determine if we have input file set
$haveUploadedFile = !empty($_POST['imagefile']);
if( $haveUploadedFile ) {
// [...] do your upload stuff here.
$update_imagefile = 'myAwesomeImage.png';
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
}
else {
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext' WHERE `imageid`='$update_id'"; //delete query
}
$run = mysqli_query($db, $update_query);
//[...]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.