这是我的编辑页面更新脚本。我有个问题。在每次更新时,脚本都会更新映像名称并插入MySQL。我希望该系统在文件名为NULL(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");
?>
答案 0 :(得分:0)
你可以这样做,比如使用简单的if条件
if($pic_name != null AND isset($_FILES['image'])){
$q->execute(array($title,$content,$pic,$id));
}
答案 1 :(得分:0)
如果你不需要,首先不要生成文件名并创建一个Upload对象。
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);