Jump to content


Photo

if..else inside another if..else statment? will that work.. check this script


  • Please log in to reply
11 replies to this topic

#1 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 23 October 2006 - 12:12 AM

im getting a T_ELSE error so im assuming that you cant do it... what do you think.. heres my script...

<?

/* Account activation script */

// Get database connection

include ( './db.php' );

// Create variables from URL.

// first check if it's already been activated

$sql = mysql_query ( "SELECT COUNT(*) AS total FROM users WHERE userid = '" . mysql_real_escape_string ( $_REQUEST['id'] ) . "' AND password = '" . mysql_real_escape_string ( $_REQUEST['code'] ) . "' AND activated = 1" ) or die ( 'Query Error: ' . mysql_error );

$found = mysql_fetch_assoc ( $sql );

if ( $found['total'] == 0 )
{
	$sql = mysql_query ( "UPDATE users SET activated = 1 WHERE userid = '" . mysql_real_escape_string ( $_REQUEST['id'] ) . "' AND password = '" . mysql_real_escape_string ( $_REQUEST['code'] ) . "'" ) or die ( 'Query Error: ' . mysql_error );

	if ( mysql_affected_rows ( $sql ) == 0 ) 
        {
        die(mysql_error());
        }
	{
		echo "<strong><font color='red'>Your account could not be activated, no user found by that id or password!</font></strong>";
	}
	else
	{
		echo "<strong>Your account has been activated!</strong> You may login below!<br />";

    		include ( './login.php' );
	}
}
else
{
	echo "<strong>You have already activated your account!</strong> You may login below!<br />";

    	include ( './login.php' );
}

?>


#2 Design

Design
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationThe Computer Chair

Posted 23 October 2006 - 12:18 AM

You and I are both in the same boat, and I think the solution lies within either the Case or Switch functions, I'm going to look and see if w3schools.com has some reference to this, so I'll let you know if I find anything.

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 23 October 2006 - 12:21 AM

You can't use die and then output info for the user!!!

That script is simply not structureed well so you have missing closing tags etc etc.

Learn to indent you code more effectively - it will help you spot erros.

This is what I think you trying to do (I have removed the die(mysql_error()); line as it is not needed - if there were and error you'd never get to that line..)
if ( $found['total'] == 0 )
{
	$sql = mysql_query ( "UPDATE users SET activated = 1 WHERE userid = '" . mysql_real_escape_string ( $_REQUEST['id'] ) . "' AND password = '" . mysql_real_escape_string ( $_REQUEST['code'] ) . "'" ) or die ( 'Query Error: ' . mysql_error );
	
	if ( mysql_affected_rows ( $sql ) == 0 ) 
	{
		echo "<strong><font color='red'>Your account could not be activated, no user found by that id or password!</font></strong>";
	}
	else
	{
		echo "<strong>Your account has been activated!</strong> You may login below!<br />";
		
		include ( './login.php' );
	}
}
else
{
	echo "<strong>You have already activated your account!</strong> You may login below!<br />";
	
	include ( './login.php' );
}

follow me on twitter @PHPsycho

#4 Design

Design
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationThe Computer Chair

Posted 23 October 2006 - 12:23 AM

Okay, i'm not 100% sure, but I think you could pull it off using If().... elseif().... elseif().... else()

#5 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 23 October 2006 - 12:26 AM

thanks design.. ill mess around with that and see if i can get it to work.. i think your right.. first im gonna see what toon meant with that script and try to get this to work..

#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 23 October 2006 - 12:28 AM

No his structure is correct try to avoid comparing completely different elemenst in an else if statement - if only to avoid confusion.

if you are looking at one compariosn with many potential values that control code flow then use the switch statement but in this case it is not necessary
follow me on twitter @PHPsycho

#7 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 23 October 2006 - 12:35 AM

nope im still getting the same error with your script toon..

#8 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 23 October 2006 - 12:43 AM

in the query line...

you are missing '()' after mysql_error
follow me on twitter @PHPsycho

#9 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 23 October 2006 - 12:48 AM

This is how I would have done it - you shoudl find it a bit easier to read....

<?php
if	(
	$found['total']	==	0
	)
{
	$sql	=	"
				UPDATE
					users
				SET
					activated	=	1
				WHERE
					userid		=	'" . mysql_real_escape_string ( $_REQUEST['id'] ) . "'
				AND
					password	=	'" . mysql_real_escape_string ( $_REQUEST['code'] ) . "'
					";
	$sql	=	mysql_query($qry)
				or
				die ( 'Query Error: ' . mysql_error());
	if ( mysql_affected_rows ( $sql ) == 0 ) 
	{
?>
			<strong><font color="red">Your account could not be activated, no user found by that id
			or password!</font></strong>";
<?php
	}
	else
	{
?>
			<strong>Your account has been activated!</strong> You may login below!<br />";
<?php		
		include ( './login.php' );
	}
}
else
{
?>
			<strong>You have already activated your account!</strong> You may login below!<br />";
<?php	
	include ( './login.php' );
}
?>

well if it didn't bugger up teh tabs so much ;)
follow me on twitter @PHPsycho

#10 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 23 October 2006 - 12:54 AM

thanks man.. i used your code and this came up...

Query Error: Query was empty

would you know what that means and how to fix it?

#11 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 23 October 2006 - 01:00 AM

echo the query to teh screen and check it in phpmyadmin or something - see if it returns any rows or highligths any specific error.
follow me on twitter @PHPsycho

#12 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 23 October 2006 - 01:14 AM

in phpmyadmin its normal so i dont know whats wrong with it




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users