Moorcam Posted September 24, 2021 Share Posted September 24, 2021 Hi guys, Using the following code to upload an image to a directory. It aint being funky... if ($_FILES['image']['error'] = UPLOAD_ERR_OK) { // image file directory $target = "images/uploads/".basename($_FILES['image']['name']); if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { $msg = "Image uploaded successfully"; $post['image'] = $target; }else{ $msg = "Failed to upload image"; $post['image'] = null; } } else { $post['image'] = null; } Can anyone tell me what's up? Thanks guys Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted September 24, 2021 Solution Share Posted September 24, 2021 Step away from the computer, make yourself a sandwich or watch some TV or whatever you like, then come back to your code and look at the first line. Quote Link to comment Share on other sites More sharing options...
Moorcam Posted September 24, 2021 Author Share Posted September 24, 2021 3 minutes ago, requinix said: Step away from the computer, make yourself a sandwich or watch some TV or whatever you like, then come back to your code and look at the first line. Had the sandwich and the coffee. No smoke because I gave them up about 2 weeks ago (God help everyone around me) and had a look at the first line. Changed it to the following: if(!empty($_FILES['image']['name'])) { Works perfectly. No idea why that didn't shout at me earlier. Thank you kindly. Quote Link to comment Share on other sites More sharing options...
requinix Posted September 24, 2021 Share Posted September 24, 2021 2 minutes ago, Moorcam said: if(!empty($_FILES['image']['name'])) { Works perfectly. No idea why that didn't shout at me earlier. Perhaps because what you had before was better. In fact it wasn't just better, it was correct. All it was missing was an equals sign. Quote Link to comment Share on other sites More sharing options...
Moorcam Posted September 24, 2021 Author Share Posted September 24, 2021 Thanks heaps. Now, if there is a scenario where the form needs to be updated but without uploading a new image, at this time it just gets rid of the current image. I need to work out how to prevent that. Previously, before moving to PDO, I had a second insert query inside an if clause. Tried it with this but got an UNEXPECTED else. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 24, 2021 Share Posted September 24, 2021 If you change the query to this $sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, position = :position, role = :role, submittedby = :submittedby, image = COALESCE(:image, image) WHERE id = :id "; then, if :image is NULL, the existing value will not be changed. Quote Link to comment Share on other sites More sharing options...
Moorcam Posted September 24, 2021 Author Share Posted September 24, 2021 6 minutes ago, Barand said: If you change the query to this $sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, position = :position, role = :role, submittedby = :submittedby, image = COALESCE(:image, image) WHERE id = :id "; then, if :image is NULL, the existing value will not be changed. Thank you so much man. Really appreciate it. Promise I won't bother you anymore ... until the next issue Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.