Page 1 of 1

elseif not working correctly

Posted: Sun Jul 29, 2007 8:25 am
by pinehead18

Code: Select all

if(isset($sec)) { $sql = "SELECT * FROM posts WHERE id='$id' AND section='$sec' ORDER BY id DESC"; } 
	
elseif($sec == "showall") {

	 $sql = "SELECT * FROM posts WHERE id='$id' ORDER BY id DESC";
	 
 }
var_dump($sec) shows that showall is indeed set.

I also changed it to 1 = 1 just to see and it still wouldn't work.

How did i mess this up?


thanks guys

Posted: Sun Jul 29, 2007 8:28 am
by feyd
If $sec is set, the true statements will be run.

Posted: Sun Jul 29, 2007 8:58 am
by superdezign
Your else if is impossible to reach.

You're essentially doing this:

Code: Select all

if($i > 1)
{
    // Do something
}
else if ($i > 2)
{
    // If $i isn't bigger than 1, it's not bigger than 2
    // and if it is bigger than 1, it'll never get here
}

Posted: Sun Jul 29, 2007 1:31 pm
by miro_igov
I never seen elseif not working correctly :)

Posted: Sun Jul 29, 2007 4:01 pm
by Benjamin
miro_igov wrote:I never seen elseif not working correctly :)
It is working correctly, your logic is flawed and your indentation is off. Your query is also open to injection as well.

Code: Select all

if(isset($sec))
{
    if ($sec == 'showall')
    {
        $sql = "SELECT * FROM posts WHERE id='$id' ORDER BY id DESC";
    } else {
        $sql = "SELECT * FROM posts WHERE id='$id' AND section='$sec' ORDER BY id DESC"; 
    }
} else {
    // $sec is not set
}