Page 1 of 1

Problems with error messaging

Posted: Sat Jan 03, 2009 2:33 pm
by Icethrill
So I am learning php at the moment. I am following a tutorial. I am programming a simple CMS system for a website. I have a form so you can edit some rows in the database. Like changing the name, position and if its visible or not. Atm I am fairly gotten into the error messaging. And I want to recieve an error message if I leave the menu_name field empty. But it changes the name to nothing and the form succeeds.

Here is the code thats giving me problems:

Code: Select all

if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)){
            $errors[] = $fieldname;
        }
(empty($_POST[$fieldname]) && $_POST[$fieldname] != 0) It works fine if I delete the $_POST[$fieldname] != 0 but I really need that condition with the &&. I am 100% sure I have written the code correctly since I trippled checked with the tutorial. I just dont understand whats the problem. Anyone having an idea? Have I missed something in the condition?

Re: Problems with error messaging

Posted: Sat Jan 03, 2009 2:50 pm
by kaszu
Checking any value against 0 with != is useless since it will evaluate to true only if value will be 0, false, empty or null, which in this case will be duplication of what empty() function does (almost). So basically

Code: Select all

empty($_POST[$fieldname]) && $_POST[$fieldname] != 0
is almost the same as

Code: Select all

empty($_POST[$fieldname]) && !empty($_POST[$fieldname])
So it never evaluates to true and is useless, so only part of condition which ma yevaluate to true is isset(...)

$_POST value is always a string or null (not defined), so maybe instead of '!= 0' you mean '!= "0"' ?
In that case condition will make sense. If it's not the case please explain why != 0 is needed.

Re: Problems with error messaging

Posted: Sat Jan 03, 2009 4:15 pm
by Icethrill
kaszu wrote:Checking any value against 0 with != is useless since it will evaluate to true only if value will be 0, false, empty or null, which in this case will be duplication of what empty() function does (almost). So basically

Code: Select all

empty($_POST[$fieldname]) && $_POST[$fieldname] != 0
is almost the same as

Code: Select all

empty($_POST[$fieldname]) && !empty($_POST[$fieldname])
So it never evaluates to true and is useless, so only part of condition which ma yevaluate to true is isset(...)

$_POST value is always a string or null (not defined), so maybe instead of '!= 0' you mean '!= "0"' ?
In that case condition will make sense. If it's not the case please explain why != 0 is needed.
Thank you! It worked when I used "0". :) Weird that the other thing worked for the dude that made the video tutorial.