我有多个图像上传选项的形式,他们工作良好,并在mysql数据库列更新文件名?
当我上传image1然后图像移动到服务器上,它也显示在html table columns
旁边,现在的问题是,当我上传image with file option2
后表单提交然后image1 which is on the next to the image1 file option
将消失,在mysql database image1 columns will be blank
?
多个图片上传功能
$id=$_REQUEST['id'];
if(isset($_POST['submit']))
{
if (!empty($_FILES['image']['name']))
{
$rnd_1 = rand(11111,99999);
$file_name= $rnd_1.'_'.$_FILES['image']["name"];
$file_path = "uploads/";
$image = new imgMark();
$image->font_path = "arial.ttf";
$image->font_size = 25;
$image->water_mark_text = "© www.edge.pk";
$image->color = 'CC003E';
$image->opacity = 50;
$image->rotation = 0;
if($image->convertImage('image', $file_name, $file_path))
$demo_image = $image->img_path;
}
if (!empty($_FILES['image1']['name']))
{
$rnd_1 = rand(11111,99999);
$file_name= $rnd_1.'_'.$_FILES['image1']["name"];
$file_path = "uploads/";
$image = new imgMark();
$image->font_path = "arial.ttf";
$image->font_size = 35;
$image->water_mark_text = "© www.edge.pk";
$image->color = 'CC003E';
$image->opacity = 50;
$image->rotation = 0;
if($image->convertImage('image1', $file_name, $file_path))
$demo_image2 = $image->img_path;
}
if (!empty($_FILES['image2']['name']))
{
$rnd_1 = rand(11111,99999);
$file_name= $rnd_1.'_'.$_FILES['image2']["name"];
$file_path = "uploads/";
$image = new imgMark();
$image->font_path = "arial.ttf";
$image->font_size = 35;
$image->water_mark_text = "© www.edge.pk";
$image->color = 'CC003E';
$image->opacity = 50;
$image->rotation = 0;
if($image->convertImage('image2', $file_name, $file_path))
$demo_image3 = $image->img_path;
}
更新查询UPDATE products SET
image='$demo_image',addimage1='$demo_image2',addimage2='$demo_image3'
WHERE id='$id'
}
Select Images Query
$query1=mysql_query("select images,addimages1,addimages2 from products
where id='$id' ")or die("query");
$row2=mysql_fetch_array($query1);
<form method="post" enctype="multipart/form-data">
Image Upload Option1
<input type="file" name="image" id="image" />
<img src="<?php echo $image['image'] ?>" width="150" height="150" />
Image Upload Option2
<input type="file" name="image1" id="image1"/>
<img src="<?php echo $image['addimage1'] ?>" width="150" height="150" />
Image Upload Option3
<input type="file" name="image2" id="image2"/>
<img src="<?php echo $image['addimage2'] ?>" width="150" height="150" />
<input type="submit" class="bg" name="submit" />
</form>
问题在于:
UPDATE products SET
image='$demo_image',addimage1='$demo_image2',addimage2='$demo_image3'
WHERE id='$id'
}
根据我的理解,你首先提交你上传第一张图片的表单,然后你再次提交你上传第二张图片的表单。这样做的原因是,在第二次上传期间,变量$demo_image将为空,因为在重新提交期间没有发送输入"image"的任何值。在这里看到的:
$demo_image = $image->img_path;
$image->img_path为空,因此$demo_image将为空(或NULL?)-不确定)也是。
这个问题有很多解决办法。您可以从数据库中检索数据到表单,然后重新提交,或者测试变量是否为空白(或NULL?),然后对不同的变量使用不同的UPDATE命令,或者在运行UPDATE之前从MySQL检索数据等等。
我会选择从MySQL中检索"旧"数据,然后测试它们是否为NULL。如果它们在MySQL中不是NULL,你只需要重新提交相同的数据到你的数据库。这样的:
if ($demo_image_from_db!=NULL) $demo_image = $demo_image_from_db;
也有可能有一个MySQL本地函数更新只有当NULL -我听说过COALESCE,但我不确切地知道如何使用它-这将绝对是最简单的方法来做到这一点