Jump to content

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


jwk811

Recommended Posts

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

[code]<?

/* 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' );
}

?>[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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..)
[code]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' );
}[/code]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

[code]
<?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' );
}
?>
[/code]

well if it didn't bugger up teh tabs so much ;)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.