PHP& MySQL提交错误消息问题

时间:2010-04-15 19:08:44

标签: php mysql

当我提交新名称而不是新头像时,我会收到以下头像错误消息Please upload a .gif, .jpeg, .jpg or .png image!。我希望能够只发送一个新名称,而无需每次提交表单时都上传新的头像而不会收到头像错误消息Please upload a .gif, .jpeg, .jpg or .png image!有人可以帮我解决这个问题吗?

这是php代码。

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 个答案:

答案 0 :(得分:0)

您只需添加一项检查,看看他们是否为其头像指定了新图像(或将其留空)。在这种情况下,在不知道如何设置表单的情况下,您可以简单地检查大小是否大于零(在这种情况下,isset()/ empty()可以返回意外结果):

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

修改 在回复您的评论时,您将更改以下行(包括修复基本逻辑错误):

} 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") {

为:

} 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") {