如果imagefile为空,则验证对非空文件也有单独的查询

时间:2018-04-09 11:33:33

标签: php

我希望有一个单独的查询,当图像文件留空时只更新imagename和imagetext,并在用户选择新图像时更新所有imagename,imagetext和图像本身是我的查询:

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

1 个答案:

答案 0 :(得分:0)

确定你是否有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);
    //[...]

}