If-Else语句错误

时间:2014-04-03 13:24:25

标签: php

程序应该是这样的:

  • 代码将检查是否有可用于上传的文件。
  • 如果找到任何文件。它将上传,文本将保存在数据库中。
  • 但如果找不到文件,它将不会上传,路径也不会保存,但文本仍会保存在数据库中。

真正发生的事情:

  • 正在保存文字
  • 图像路径仍在保存!因此,在后期检索图像会有问题。

你注意到了:

  • 我删除了avatar_image_path上的if,这就是我想到的图像路径不能保存但它仍在保存。

代码:

 if ($_FILES["file"]["name"] == 0)
    {

    $updateQuery = "UPDATE  `users`.`info` SET  `password` =  '$newPass',`contact_number` =  '$newConNum',`user_address` =  '$newAdd',`email_address` = '$newEmail', WHERE  `info`.`username` =  '$username' AND  `info`.`password` =  '$userpass' AND `info`.`firstname` =  '$firstName' AND  `info`.`lastname` =  '$lastName' AND  `info`.`admin_level` =$adminLvl AND  `info`.`contact_number` =  '$ConNum' AND  `info`.`user_address` =  '$usrAdd' AND `info`.`avatar_image_path` =  '$avaImgPth' ";
    $executeQuery=$con->query($updateQuery);
    echo "Profile successfully UPDATED!";

      }

 else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"], "avatar/owner-".$_SESSION['username']."-fname-".$_SESSION['fname']."-l_name-".$_SESSION['lname']."-filename-".$_FILES["file"]["name"]);
    $updateQuery1 = "UPDATE  `users`.`info` SET  `password` =  '$newPass',`contact_number` =  '$newConNum',`user_address` =  '$newAdd',`email_address` = '$newEmail',`avatar_image_path` =  '$filepath' WHERE  `info`.`username` =  '$username' AND  `info`.`password` =  '$userpass' AND `info`.`firstname` =  '$firstName' AND  `info`.`lastname` =  '$lastName' AND  `info`.`admin_level` =$adminLvl AND  `info`.`contact_number` =  '$ConNum' AND  `info`.`user_address` =  '$usrAdd' AND `info`.`avatar_image_path` =  '$avaImgPth' ";
    $executeQuery1=$con->query($updateQuery1);

    echo "Profile successfully UPDATED!";
    }

1 个答案:

答案 0 :(得分:0)

首先,您必须在隐藏字段中保存旧的个人资料图片。

<html>
<input type="hidden" name="old_image" value=<?php echo $row['image'];?>/>
<input type="file" name="new_image"/>
</html>

现在当您提交表格时,请检查

<?php
if($_FILES['new_image']['name']!="" && $_POST['old_image']=="")
{
echo "upload process...";
}
else
{
echo "save old image in database.";
}
?>