0

我正在提交一个表格,我正在更新state id,city namecity image.

当我只更新图像时,它可以工作。当我更新state idcity name希望我的旧图像保持不变时,数据库中的照片字段变为空白。

我的 PHP 代码是这样的:

 <?php 

if(isset($_POST) && $_POST['submit'] == "Update")
{
        extract($_POST);
        if($_FILES['photo'])
        {
        $cityimg = upload_file($_FILES['photo'],'cityimg/','image','N','true','thumb/', 100, 100);
        $sql = "UPDATE city SET mcid = '$mcid', city_name = '$city_name', photo = '$cityimg' WHERE cid = '$cid'";
        }
        else
        {
        $sql = "UPDATE city SET mcid = '$mcid', city_name = '$city_name' WHERE cid = '$cid'";
        }
        $result = mysql_query($sql);
        if($result)
        {
            $msg = "City Updated Successfully.";
        }
}

?>

我认为我的循环有问题。

4

1 回答 1

1

You should test if a file was uploaded in a different way. For example, this will make sure that no error occurred during the file upload (and if there was any) :

if($_FILES['photo']['error'] == UPLOAD_ERR_OK){
    // A file was uploaded and there is no error
}

If you only want to test if no file was selected, you could use UPLOAD_ERR_NO_FILE.

More info on file upload error messages

This should work for you :

<?php 

if(isset($_POST) && $_POST['submit'] == "Update")
{
        extract($_POST);

        // If image was uploaded
        if($_FILES['logo']['error'] == UPLOAD_ERR_OK)
        {
            $cityimg = upload_file($_FILES['photo'],'cityimg/','image','N','true','thumb/', 100, 100);
            $sql = "UPDATE city SET mcid = '$mcid', city_name = '$city_name', photo = '$cityimg' WHERE cid = '$cid'";
        }

        // If no image was uploaded
        else
        {
            $sql = "UPDATE city SET mcid = '$mcid', city_name = '$city_name' WHERE cid = '$cid'";
        }
        $result = mysql_query($sql);
        if($result)
        {
            $msg = "City Updated Successfully.";
        }
}

Check this post for more details

于 2012-11-22T10:51:23.990 回答