douyi3767 2010-04-15 19:08 采纳率: 0%
浏览 48
已采纳

PHP和MySQL提交错误消息问题

When I submit a new name and not a new avatar I get the following avatar error message Please upload a .gif, .jpeg, .jpg or .png image!. I want to be able to send a new name only without having to upload a new avatar each time I submit the form without getting the avatar error message Please upload a .gif, .jpeg, .jpg or .png image! can someone help me fix this problem?

Here is the php code.

if (isset($_POST['submitted'])) {

    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT users.*
                                 FROM users 
                                 WHERE user_id=3");

    $first_name = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_POST['first_name'])));

$user_id = '3';

if(isset($_FILES["avatar"]["name"]) && $_FILES['avatar']['size'] <= 5242880) {

    if($_FILES["avatar"]["type"] == "image/gif" || $_FILES["avatar"]["type"] == "image/jpeg" || $_FILES["avatar"]["type"] == "image/jpg" || $_FILES["avatar"]["type"] == "image/png" || $_FILES["avatar"]["type"] == "image/pjpeg") {

        if (file_exists("../members/" . $user_id . "/images/" . $_FILES["avatar"]["name"])) {
          echo '<p class="error">' . mysqli_real_escape_string($mysqli, htmlentities(strip_tags(basename($_FILES["avatar"]["name"])))) . ' already exists! ';
        } else if($_FILES["avatar"]["name"] == TRUE) {
          move_uploaded_file($_FILES["avatar"]["tmp_name"],
          "../members/" . $user_id . "/images/" . mysqli_real_escape_string($mysqli, htmlentities(strip_tags(basename($_FILES["avatar"]["name"])))));
          $avatar = mysqli_real_escape_string($mysqli, htmlentities(strip_tags(basename($_FILES["avatar"]["name"]))));
        }

    } else if($_FILES["avatar"]["type"] != "image/gif" || $_FILES["avatar"]["type"] != "image/jpeg" || $_FILES["avatar"]["type"] != "image/jpg" || $_FILES["avatar"]["type"] != "image/png" || $_FILES["avatar"]["type"] != "image/pjpeg") {
        echo '<p class="error">Please upload a .gif, .jpeg, .jpg or .png image!</p>';
    }

} else if($_FILES['avatar']['size'] >= 5242880) {
    echo '<p class="error">Please upload a smaller pic!</p>';
} else if($_FILES["avatar"]["name"] == NULL) {
    $avatar = NULL;
}


if(isset($_FILES["avatar"]["name"]) && $_FILES['avatar']['size'] <= 5242880) {

        if (mysqli_num_rows($dbc) == 0) {
                $mysqli = mysqli_connect("localhost", "root", "", "sitename");
                $dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, first_name, avatar) 
                                             VALUES ('$user_id', '$first_name', '$avatar')");
        }

        if ($dbc == TRUE) {
                $dbc = mysqli_query($mysqli,"UPDATE users 
                                             SET first_name = '$first_name', avatar = '$avatar' 
                                             WHERE user_id = '$user_id'");

                echo '<p class="changes-saved">Your changes have been saved!</p>';

        }

        if (!$dbc) {
                print mysqli_error($mysqli);
                return;
        }

    }

}
  • 写回答

1条回答 默认 最新

  • douren7921 2010-04-15 19:13
    关注

    You simply need to add a check to see if they specified a new image for their avatar (or left it blank). In that case, without knowing how your form is set up, you could simply check that the size is greater than zero (isset()/empty() can return unexpected results in this case):

    if($_FILES['avatar']['size'] > 0) { ... }
    

    Edit In response to your comment, you'd change the following line (including fixing a basic logic error):

    } else if($_FILES["avatar"]["type"] != "image/gif" || $_FILES["avatar"]["type"] != "image/jpeg" || $_FILES["avatar"]["type"] != "image/jpg" || $_FILES["avatar"]["type"] != "image/png" || $_FILES["avatar"]["type"] != "image/pjpeg") {
    

    to:

    } else if($_FILES["avatar"]["size"] <= 0 && $_FILES["avatar"]["type"] != "image/gif" && $_FILES["avatar"]["type"] != "image/jpeg" && $_FILES["avatar"]["type"] != "image/jpg" && $_FILES["avatar"]["type"] != "image/png" && $_FILES["avatar"]["type"] != "image/pjpeg") {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制