Php 如果文件设置为don';不变

Php 如果文件设置为don';不变,php,image,edit,isset,Php,Image,Edit,Isset,这是我的编辑页面更新脚本。我有个问题。每次更新时,脚本都会更新图像名称并将其插入MySQL。我希望系统不插入或更新文件名,如果它是空的(isset)。以下是我的代码: <?php require 'aed-config.php'; require 'class.upload.php'; function gen_random_string($length=16) { $chars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq

这是我的编辑页面更新脚本。我有个问题。每次更新时,脚本都会更新图像名称并将其插入MySQL。我希望系统不插入或更新文件名,如果它是空的(isset)。以下是我的代码:

    <?php
require 'aed-config.php';
require 'class.upload.php';


function gen_random_string($length=16)
{
    $chars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";//length:36
    $final_rand='';
    for($i=0;$i<$length; $i++)
    {
        $final_rand .= $chars[ rand(0,strlen($chars)-1)];

    }
    return $final_rand;
}
$pic_name = gen_random_string();

$image = new Upload( $_FILES[ 'image' ] );
    if ( $image->uploaded ) {

        $image->file_new_name_body = $pic_name;
        $image->image_convert = 'jpg';
        $image->image_resize = true;
        $image->image_ratio_crop = true;
        $image->image_x = 460;
        $image->image_y = 300;
        $image->Process( '../img' );

        $image->allowed = array ( 'image/*' );
    }


$pic = $pic_name.'.jpg';
$title = $_POST['title'];
$content = $_POST['content'];
$id = $_POST['memids'];

$sql = "UPDATE about_us 
        SET title=?, content=?, pic=?
        WHERE id=?";
$q = $db->prepare($sql);
$q->execute(array($title,$content,$pic,$id));
header("location: about.php");

?>

您可以像使用简单的if条件一样执行此操作

if($pic_name != null AND isset($_FILES['image'])){
    $q->execute(array($title,$content,$pic,$id));
}

首先,如果不需要,不要生成文件名并创建上载对象

if ($_FILES['image'])
{
    // Generate filename and create your Upload object as well as the $pic variable
}
然后是查询部分

$executeArray = array($title, $content);
$sql = "UPDATE about_us
        SET title = ?, content = ?, ";

if (isset($pic)
{
    $executeArray[] = $pic;
    $sql .= "pic = ?, ";
}

$sql .= "WHERE id = ?";
$executeArray[] = $id;

$q->execute($executeArray);

只需检查是否设置了
$\u FILES['image']
,然后更新pic,或者重新分配旧值,或者不在更新查询中更新它。。