Page 1 of 1
[SOLVED] Bug in if else...
Posted: Sun Jul 04, 2004 12:34 pm
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
Posted: Sun Jul 04, 2004 12:44 pm
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.
Posted: Sun Jul 04, 2004 12:55 pm
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
Posted: Sun Jul 04, 2004 12:58 pm
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

)
Posted: Sun Jul 04, 2004 1:03 pm
by phice
You really need to learn to use parenthesis in your if/elseif/else statements.
Posted: Sun Jul 04, 2004 1:13 pm
by feyd
is the second argument to update() a reference?
Posted: Sun Jul 04, 2004 1:27 pm
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
Posted: Sun Jul 04, 2004 1:30 pm
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)
{....}
?>
Posted: Sun Jul 04, 2004 1:35 pm
by tomfra
Code: Select all
function update($query, $result, $echo = false, $back = false)
{...}
Posted: Sun Jul 04, 2004 1:39 pm
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)
Posted: Sun Jul 04, 2004 1:51 pm
by tomfra
I changed it but no difference...
Posted: Sun Jul 04, 2004 2:22 pm
by tomfra
I solved it
I simply defined $result2 as:
...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