无法找到我的逻辑错误:我正在测试的条件永远不会成立

时间:2012-06-28 00:14:19

标签: php mysql if-statement

这是一个结果页面,它接收有关用户在上一页的表单中选中的框的一些信息。它根据用户名的选择采用该信息并更新数据库中的值。我在底部遇到问题,我的if / else if / else语句中的条件永远不会被激活,即使我认为它应该。你能帮我找一下我的逻辑错误吗?

它发生在靠近底部的行

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

这是不对的。如果该语句的计算结果为true,则会在数据库的表中插入一个新行。相反,如果$ iclei为真并且$ icleiDB为false(这应该使语句成立为真),我得到“iclei仍为真”的结果,这只应该在$ iclei为真且$ icleiDB为真时发生

注意:我正在使用一个名为MySqlConnection.php的PHP文件,它简化了与数据库的连接,所以无论何时你看到$ mySqlConnection-> doQuery()或$ mySqlConnection-> doNonQuery(),这都是对数据库,括号中的任何内容都是查询。

以下是所有代码:

<?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>

2 个答案:

答案 0 :(得分:0)

男人,我试着读这个,但它让我头疼......也许这也是你不“看”它的原因......

我没有看到错误,但有些事情可以帮助你解决问题:

  1. 在php中有切换,不要做这样的疯狂if / else / elsefi的东西。
  2. 将您的代码封装在方法,类 - &gt;阅读“面向对象的编程” - 真的它会节省你的长期时间......如果没有它你就不会完成..这是意大利面条代码...如果你在几周后得到这个代码你必须想到一切......
  3. 永远不要在sql querys中使用userinput! - &GT;准备好sql注入!

答案 1 :(得分:0)

您设置的变量名为$iclei,但if条件中的变量名为$iceli。注意拼写差异。为了让PHP警告你这样的错误,你可能想看一下this question的答案。