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;
}
}
}