Jump to content

Archived

This topic is now archived and is closed to further replies.

jwk811

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

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]

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Okay, i'm not 100% sure, but I think you could pull it off using If().... elseif().... elseif().... else()

Share this post


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

Share this post


Link to post
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

Share this post


Link to post
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 ;)

Share this post


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

Share this post


Link to post
Share on other sites
echo the query to teh screen and check it in phpmyadmin or something - see if it returns any rows or highligths any specific error.

Share this post


Link to post
Share on other sites

×

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.