PHP IF Statement Problems With isset & !empty

Hello all!

I am just stuck today at a wall of confusion and I'm hoping someone will be able to assist :)

I have a database full of basic projects, and within that table are attributes like Project Name, Project Number, Project Image, etc. I am able to enter new projects / display existing projects / etc. without issue.

My problem seems to come up when I want to Edit a project. My thoughts were that I would have to create an IF statement to find out if there's a new file uploaded or not, and either set the new file name in the database if there is, or keep the old in the database if there isn't.

I've been playing around with this for days, and I think I started off a bit too far ahead of myself. I've started breaking down to basics and I'm getting stumped with my IF statement.

Examples:
if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(isset($OldProjectImage1)){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

Open in new window


if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(!empty($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

Open in new window


I've tried both with my script, and they're both behaving similarly. Perhaps I am just confused at the overall process?

When I run my tests with and without cookies enabled, it always seems to skip the first part of the IF statement (with both isset and !empty) and jump to the next section. Then, similarly, it feels like the IF statement is backwards (if that makes any sense) - if I set a file to upload which populates ProjectImage1, I get "Image1 FILES empty". If I set no file to upload and submit the form, I get "Image1 FILES isset".

I thought it would essentially be, in plain English,
If cookie is empty then echo "Your Browser Cookies are not enabled"
Else if ProjectImage1 Name is set, echo "Image1 FILES isset"
Else if ProjectImage1 Name is Empty, echo "Image1 FILES empty"

Open in new window

but it's feeling to me like it's backwards? Am I understanding this wrong?

Thanks in advanced for any responses!
Chris WaddiloveI.T. TechnicianAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
//If you actually have a cookie named OldProjectImage, then this if clause will get executed
//and $OldProjectImage1 will have the value of the cookie.
//Let's say that $_COOKIE["OldProjectImage1"]="image1.png";
// so $OldProjectImage1=="image1.png"
if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

//now here you are saying if there IS something in $OldProjectImage1
//then echo 'Your Browser Cookies are not enabled'!  It should be the inverse.  Put a ! infront
if(isset($OldProjectImage1)){ 
echo 'Your Browser Cookies are not enabled';
}

Open in new window


Try:
if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if( ! isset($OldProjectImage1)){
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES isset';
} else if( empty($_FILES['ProjectImage1']['name'])){
//notice that there should be no ! infront of empty
echo 'Image1 FILES empty';
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris WaddiloveI.T. TechnicianAuthor Commented:
Thank you so much for the explanation - I'm still new to this whole aspect and trying to hack things together from examples, I was not aware of the difference of adding the ! to the string. That's exactly what I was looking for! I had a feeling it was something silly as such.
Ray PaseurCommented:
If you're new to PHP and want to find some dependable learning resources and examples, this article may help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.