Jump to content


Photo

If Statment running wrong?


  • Please log in to reply
5 replies to this topic

#1 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 09 October 2006 - 09:14 PM

Hey, Ive been setting up a user managment system,
And i have a page that allows users to edit their info, as you would,

But after letting them fill the form, i stored their new information in variables! and now i want to make sure that nothing they entered is NULL or empty!

//Check If The Updates Are Legal!
if ($newusername !== NULL || $newpassword !== NULL || $newemail !== NULL) {
$doupdates = "TRUE";
} else {
$doupdates = "FALSE";
}


So if none of them are NULL, it will do the updates, else dont do them,

But this isnt working because later down the script i have...

if ($doupdates == "TRUE") {
echo "Do Update: TRUE";
} elseif ($doupdates == "FALSE") {
echo "<font color='#FF0000'><center><b>Please Note:<br>There Has Been An Error In The Information You Entered!<br>Please Go Back And Try Again!</b></center></font>";
}


I replaced my sql updates with an echo, just for small topic... but when i run the script, and i leave one or all of the form fields.. I just get The Do Update True, so what have i done wrong??

Thanks
www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 09 October 2006 - 09:17 PM

You if statement is wrong (($doupdates == "TRUE")).
You are checking if $doupdates is the string "TRUE".

You can either do-
if($doupdates)
or
if($doupdates === TRUE)

Same goes for the false, if($doupdates === FALSE) or if(!$doupdates).

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 09 October 2006 - 09:19 PM

Try this

//Check If The Updates Are Legal!
if ($newusername && $newpassword && $newemail) {
	$doupdates = true;
} 
else {
	$doupdates = false;
}

if ($doupdates === true) {
	echo "Do Update: TRUE";
} 
elseif ($doupdates === false) {
	echo "<font color='#FF0000'><center>Please Note:
	There Has Been An Error In The Information You Entered!
	Please Go Back And Try Again!</center></font>";
}

EDIT: changed the "||" (or) to "&&" (and) > I assume you want all to be entered, not just any one or more of them, change back if im wrong there
If you're not part of the solution, you're part of the precipitate

#4 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 09 October 2006 - 09:25 PM

Ok i Changed it to...

if ($doupdates === TRUE) {
echo "Do Update: TRUE";
} elseif ($doupdates === FALSE) {
echo "<font color='#FF0000'><center><b>Please Note:<br>There Has Been An Error In The Information You Entered!<br>Please Go Back And Try Again!</b></center></font>";
}


But now i get nothing,, no echos :P

EDIT:

Hey Sanfly..

Thanks, I know that the vars ARE set! i set them from my forms! i know there is a value, i want to check if the value is null, or if it actually has a value!

?
www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.

#5 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 09 October 2006 - 09:30 PM

<?php

if(empty($_POST['var']))
{
// empty
}


if(!empty($_POST['var']))
{
// NOT empty
}

?>


#6 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 09 October 2006 - 10:04 PM


Hey Sanfly..

Thanks, I know that the vars ARE set! i set them from my forms! i know there is a value, i want to check if the value is null, or if it actually has a value!

?


If the variables are empty, they will return false (or NULL), if there is a value, it returns true.  So, either

if($somevariable){
  echo "YAY";
}
elseif(!$somevariable){
echo "Bummer, its empty/null";
}

or

if(!empty($somevariable)){
echo "YAY";
}
elseif(empty($somevariable)){
echo "Bummer its empty/null";
}

should return the same thing in most cases
If you're not part of the solution, you're part of the precipitate




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users