Good day.
When I try to edit an existing image, my code works up until when no image is selected. I put in an
IF to echo a message 'Sorry, upload a picture.' but this doesn't run. The whole php is below. What I want to achieve is to run an sql with image when image is selected, and an sql without image when none is selected. please help. Thanks in anticipation.
What I have tried:
$mysqli = new mysqli ("localhost", "root", "", "dbname");
if (isset($_POST['update'])) {
$id = $_POST['id'];
$surname = $_POST['surname'];
$other_names = $_POST['other_names'];
$dob = $_POST['dob'];
$marital_status = $_POST['marital_status'];
$telephone = $_POST['telephone'];
$unit = $_POST['unit'];
$img_name = $_FILES['picture']['name'];
$img_size = $_FILES['picture']['size'];
$tmp_name = $_FILES['picture']['tmp_name'];
$error = $_FILES['picture']['error'];
if ($error === 0) {
if ($img_size > 500000) {
echo "<script>
alert('Sorry, your file is too large, it should be less than 0.5MB or 500kb.');
window.location.href='../view_members.php';
</script>";
}else {
$img_ex = pathinfo($img_name, PATHINFO_EXTENSION);
$img_ex_lc = strtolower($img_ex);
$allowed_exs = array("jpg", "jpeg", "png");
if (in_array($img_ex_lc, $allowed_exs)) {
$new_img_name = uniqid("IMG-", true).'.'.$img_ex_lc;
$img_upload_path = '../images/'.$new_img_name;
move_uploaded_file($tmp_name, $img_upload_path);
$query = "UPDATE new_members SET surname='$surname', other_names='$other_names', dob='$dob', marital_status='$marital_status',
telephone='$telephone', unit='$unit', picture='$new_img_name' WHERE id = $id";}
else{echo "<script>
alert('Sorry, only JPG, JPEG & PNG files are allowed.');
window.location.href='../view_members.php';</script>";}
if($_FILES['picture']['error'] > 0) {
echo "<script>
alert('Sorry, upload a picture.');
window.location.href='../view_members.php';</script>";
}
}
if($mysqli->query($query)){
header("location:../view_members.php?project_edit=success");
}
}
}
?>