Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

PHP
$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");
}  
}
}
?>
Posted
Updated 16-Mar-21 6:05am
v3
Comments
Andre Oosthuizen 18-Mar-21 10:40am
   
Your if statement is in the wrong place which is why it does not run.

Your first part where the SELECT statement is, is the part that runs else it checks if the image is jpeg etc., in the same else block you check for an error...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900