[SOLVED] Bug in if else...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

[SOLVED] Bug in if else...

Post by tomfra »

There is apparently a little bug in the "if else" statement below:

Code: Select all

if ($result == FALSE && $sendvalue){
 ($message .= "<p class="error">Please import database first.</p>");
 }else if (!$_REQUEST['com'] && $sendvalue && $result == TRUE){
 ($message .= "<p class="error">You forgot to make your selection.</p>");
 }else if ($_REQUEST['com'] == 'back' && $result == TRUE){
 ($message .= "<p>All filenames have been changed to original values.</p>");
 }else if ($_REQUEST['com'] == 'update' && $result == TRUE){
 ($message .= "<p>All filenames have been changed to new, random values.</p>");
 }else if ($_REQUEST['com'] == 'db_import' && $result == TRUE){
 ($message .= "<p>Initial values have been inserted into database.</p>");
 }else{
 ($message .= "<p>Please make your selection below:</p>");
}
$result is simply a mysql_query so if it fails it thinks the db table has not been created yet and tells you to select the database import choice in the form below.

This part works great, the table is imported properly but after it's imported, it should tell you that "Initial values have been inserted into database." Instead it still tells you that you need to import the table.

But then when you refresh the page once more it works as it should have before the one extra refresh. I am sure there is a bug in the code above somewhere but I can't find it...

Thanks as always :)

Tomas
Last edited by tomfra on Sun Jul 04, 2004 2:22 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Is the importing done before this big if/else block? If the import is after this block of code then it would explain what your seeing.
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

It looks the same almost no matter when I place it.

Right now the code with the functions calls is:

Code: Select all

// Call the functions \\
if ($_REQUEST['com'] == 'db_import') {
  update ($query, $result, $echo, $back);
  db_insert ();  
}
    
if ($_REQUEST['com'] == 'update') update ($query, $result, $echo);
if ($_REQUEST['com'] == 'back') update ($query, $result, $echo, $back);

// Error & Status messages \\
 if ($result == FALSE && $sendvalue){
 ($message .= "<p class="error">Please import database first.</p>");
 }else if (!$_REQUEST['com'] && $sendvalue && $result == TRUE){
 ($message .= "<p class="error">You forgot to make your selection.</p>");
 }else if ($_REQUEST['com'] == 'back' && $result == TRUE){
 ($message .= "<p>All filenames have been changed to original values.</p>");
 }else if ($_REQUEST['com'] == 'update' && $result == TRUE){
 ($message .= "<p>All filenames have been changed to new, random values.</p>");
 }else if ($_REQUEST['com'] == 'db_import' && $result == TRUE){
 ($message .= "<p>Initial values have been inserted into database.</p>");
 }else{
 ($message .= "<p>Please make your selection below:</p>");
}
But it still doesn't work as expected.

Tomas
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Right before this line:
if ($result == FALSE && $sendvalue){

Check the value of $result, if it's FALSE after doing the import then the if/else code block is working as expected and the problem must be where the assignment of $result takes place.
var_dump($result);

When you 'do something' and it takes a refresh to see the results of 'doing something' then that's just down to a code flow/logic problem in 99% of cases, so i still think your if/else is good (though slightly ugly ;))
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

You really need to learn to use parenthesis in your if/elseif/else statements.
Image Image
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is the second argument to update() a reference?
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

Feyd,

I am not sure what you mean by "reference"... It's the name of the $result of mysql_query. This code is at the top part of the script:

Code: Select all

$query = "SELECT * FROM `{$tb_prefix}_filenames`";
$result = @mysql_query($query);
The 3rd argument in update - $echo, is there just for debugging and $back returns everything to original values.

To markl999 & phice: I know this code looks ugly and you haven't seen the remaining 200 lines ;) I have bad coding habits because I am learning php mainly from forums and a lot of those people who post there have bad coding habits so I "copy" those bad habits. It's not easy to get rid of it...

The problem with the elseif statement seems to be that after the import $result returns false while it should return true. I know it has something to do with the flow of the code but I still know so little about php or coding in general that I wonder when something I create does indeed work :) But I am learning...

Tomas
Last edited by tomfra on Sun Jul 04, 2004 1:33 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how'd you define the function update()? I just want to know the function declaration line.. i.e.

Code: Select all

<?php

function update($something, $something2, $something3)
{....}

?>
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

Code: Select all

function update($query, $result, $echo = false, $back = false)
{...}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm going to guess that you set $result to whatever comes out of mysql_query() or whatever? If so, switch update's declare to:

Code: Select all

function update($query, &$result, $echo = false, $back = false)
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

I changed it but no difference...
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

I solved it :)

I simply defined $result2 as:

Code: Select all

$result2 = @mysql_query($query);
...and put it right before the else if structure. I suppose there was a collision because of the same variable name.

Thanks for the help! I feel like I will eventually really learn it...

Tomas
Post Reply