我正在提交一份表单,我正在更新state id
,city name
和city image
。
当我只更新图像时,它可以工作。
当我更新state id
和city 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.";
}
}
?>
我认为我的循环存在一些问题。
答案 0 :(得分:1)
您应该测试文件是否以不同的方式上传。例如,这将确保在文件上传期间没有错误发生(如果有的话):
if($_FILES['photo']['error'] == UPLOAD_ERR_OK){
// A file was uploaded and there is no error
}
如果您只想测试如果没有选择文件,则可以使用UPLOAD_ERR_NO_FILE
。
More info on file upload error messages
这应该适合你:
<?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.";
}
}
查看this post了解详情