0

This is a results page that receives some information about which boxes the user checked in a form on the previous page. It takes that information and updates values in a database based on what choices the user name. I'm having an issue down at the bottom with a condition in my if/else if/else statement never being activated, even though I think it should. Can you guys help me find my logic error?

It happens down near the bottom with the line

else if($iceli && !$icleiDB){

which is never true. If that statement were evaluated true, a new line would be inserted into a table in the database. Instead, if $iclei is true and $icleiDB is false (which should make the statement come out true), I get a result of "iclei remains true," which is only supposed to happen when $iclei is true and $icleiDB is true.

Note: I'm using a PHP file called MySqlConnection.php that simplifies connecting to the database, so whenever you see $mySqlConnection->doQuery() or $mySqlConnection->doNonQuery(), that's a call to the database, and whatever's in the parentheses is the query.

Here's all of the code:

<?php

    require_once 'MySqlConnection.php';

    $server = 'localhost';
    $dataBase = 'ise_programs';
    $userName = 'root';
    $password = 'root';

    $mySqlConnection = new MySqlConnection($server, 
                                           $dataBase, 
                                           $userName, 
                                           $password);


    $townName = $_POST["townName"];
    //$townName = addslashes($townName); //To pevent SQL Injection, but make sure magic quotes are off

    $townidQuery = "SELECT townid 
                    FROM tbltowns   
                    WHERE townname = '$townName'";


    $townidArray = $mySqlConnection->doQuery($townidQuery);
    $townid = $townidArray[0][0];

    //grab values the user selected in the form
    $iclei = $_POST["ICLEI"];
    $ccef = $_POST["CCEF"];
    $ise = $_POST["ISE"];
    $ceef = $_POST["CEEF"];
    $epacc = $_POST["EPACC"];
    $k12ise = $_POST["K12ISE"];
    $kctc = $_POST["KCTC"];
    $mbbpm = $_POST["MBBPM"];
    $n2n = $_POST["N2N"];
    $sbpm = $_POST["SBPM"];


?>

<html>
    <body>

    <p>HTML works.</p>

    <p>
    townName = <?php echo($townName) ?> 
    <br />
    townid = <?php echo($townid) ?>
    </p>

    <p>Values selected by the user: </p>
    <ul>
    <li>ICLEI = <?php echo($iclei) ?></li>
    <li>CCEF = <?php echo($ccef) ?></li>
    <li>ISE = <?php echo($ise) ?></li>
    <li>CEEF = <?php echo($ceef) ?></li>
    <li>EPACC = <?php echo($epacc) ?></li>
    <li>K12ISE = <?php echo($k12ise) ?></li>
    <li>KCTC = <?php echo($kctc) ?></li>
    <li>MBBPM = <?php echo($mbbpm) ?></li>
    <li>N2N = <?php echo($n2n) ?></li>
    <li>SBPM = <?php echo($sbpm) ?></li>
    </ul>

    <p>Values from the database:</p>


    <?php

    $enabledProjectsListQuery = "SELECT projectid
               FROM tblenabledprojects
               WHERE townid = $townid";

    $enabledProjectsList = $mySqlConnection->doQuery($enabledProjectsListQuery);

    //convert enabled projects list into an array of single values
    $enabledProjects = array();
    for($i = 0; $i < count($enabledProjectsList); $i++){
        $enabledProjects[] = $enabledProjectsList[$i][0];
    }

    //print the values in the enabledProjects array
    echo('enabledProjects = ');
    foreach($enabledProjects as $i){
        echo($i . ", ");
    }

    //declare variables outside if statements
    $icleiDB = false;
    $ccefDB = false;
    $iseDB = false;
    $ceefDB = false;
    $epaccDB = false;
    $k12iseDB = false;
    $kctcDB = false;
    $mbbpmDB = false;
    $n2nDB = false;
    $sbpmDB = false;


    //determine whether values were enabled in the database (and print them)
    echo("<p>");


        if(in_array(1, $enabledProjects)){
            echo "in first if 108 ".$icleiDB."<br>";
                echo('iclei is ENABLED. <br />');
                $icleiDB = true;
            }
            else{
                echo "in else 113 ".$icleiDB."<br>";
                echo('iclei is not enabled. <br />');
                $icleiDB = false;
            }
        if(in_array(2, $enabledProjects)){
                echo('ccef is ENABLED. <br />');
                $ccefDB = true;
            }
            else{
                echo('ccef is not enabled. <br />');
                $ccefDB = false;
            }
        if(in_array(3, $enabledProjects)){
                echo('ise is ENABLED. <br />');
                $iseDB = true;
            }
            else{
                echo('ise is not enabled. <br />');
                $iseDB = false;
            }
        if(in_array(4, $enabledProjects)){
                echo('ceef is ENABLED. <br />');
                $ceefDB = true;
            }
            else{
                echo('ceef is not enabled. <br />');
                $ceefDB = false;
            }
        if(in_array(5, $enabledProjects)){
                echo('epacc is ENABLED. <br />');
                $epaccDB = true;
            }
            else{
                echo('epacc is not enabled. <br />');
                $epaccDB = false;
            }
            if(in_array(6, $enabledProjects)){
                echo('k12ise is ENABLED. <br />');
                $k12iseDB = true;
            }
            else{
                echo('k12ise is not enabled. <br />');
                $k12iseDB = false;
            }
        if(in_array(7, $enabledProjects)){
                echo('kctc is ENABLED. <br />');
                $kctcDB = true;
            }
            else{
                echo('kctc is not enabled. <br />');
                $kctcDB = false;
            }
            if(in_array(8, $enabledProjects)){
                echo('mbbpm is ENABLED. <br />');
                $mbbpmDB = true;
            }
            else{
                echo('mbbpm is not enabled. <br />');
                $mbbpmDB = false;
            }
        if(in_array(9, $enabledProjects)){
                echo('n2n is ENABLED. <br />');
                $n2nDB = true;
            }
            else{
                echo('n2n is not enabled. <br />');
                $n2nDB = false;
            }
        if(in_array(10, $enabledProjects)){
                echo('sbpm is ENABLED. <br />');
                $sbpmDB = true;
            }
            else{
                echo('sbpm is not enabled. <br />');
                $sbpmDB = false;
            }

        echo("</p>");

        if($icleiDB){
         echo "determined to be true 193 ".$icleiDB."<br>";
            echo('icleiDB is true.');
        }
        elseif(!$icleiDB){
            echo "determined to be false 197 ".$icleiDB."<br>";
            echo('icleiDB is false.');
        }

    //compare selections with DB values and decide what needs to change
    if(!$iclei && $icleiDB){ //user says iclei = false, DB says true. delete from DB
        $mySqlConnection->doNonQuery(
            "DELETE FROM tblenabledprojects
             WHERE townid = '$townid' AND projectid = '1'"
        );
        echo("Values deleted from database: ICLEI true -> false");
    }
    //echo "******iceli = ".$iceli." and icleiDB = ".$icleiDB."******<br>";
    else if($iceli && !$icleiDB){ //user says iceli = true, DB says false. add to DB
        $mySqlConnection->doNonQuery( 
            "INSERT INTO tblenabledprojects
             VALUES ('$townid', '1')"
        );
        echo("Value added to database: ICLEI false -> true");

    }
    else{
        if($iclei){
            echo("iclei remains true");
        }
        elseif(!$iclei){
            echo('iclei remains false');
        }
        else{
            echo('Who knows?');
        }
    }

    ?>
    </body>
</html>
Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
rockitude
  • 75
  • 2
  • 7

2 Answers2

0

man, i tried to read this, but it gives me headace ... maybee this is also the reason why you do not "see" it ...

i did not see the fault but some things that should help you solve the issue:

  1. there is switch in php, don't do such mad if/else/elsefi things.
  2. encapsulate your code in methos, classes -> read about "object oriented programming" -- really it will save you time in the long term ...and you wont get finished without it.. this is spaghetti code ... if you get to this code some weeks later you gotta think it all over...
  3. never use userinput in your sql querys ! -> ready about sql injection !
womd
  • 3,077
  • 26
  • 20
  • Yeah. The only thing that's striking me right away is the nested IF statements, which should probably be switch statements instead and maybe there'll be less chance for error. I already see in two places there is space missing in `else if` – Ally Jun 28 '12 at 00:51
0

The variable you're setting is called $iclei but the one in the if condition is called $iceli. Note the spelling difference. To let PHP warn you about errors like this, you might like to look at one of the answers to this question.

Community
  • 1
  • 1
Dan Hulme
  • 14,779
  • 3
  • 46
  • 95