代码没有按预期工作,类中出现了一些奇怪的错误


Code not working as intended with some weird errors in a class

基本上我的问题如下。我在浏览器中放入了以下代码?phase=1&step=0&fot=false和我得到一个黑色页面。

Apache服务器错误如下

PHP Notice: Undefined index: true in C:'Program Files (x86)'Zend'Apache2'htdocs'Proj11'1.php on line 9

这一行预期与开头的行} elseif ($_GET['true']) { $fOT = 'true'; }一样。

PHP Notice: Undefined index: true in C:'Program Files (x86)'Zend'Apache2'htdocs'Proj11'1.php on line 138

这条线与之前相同,但在第2阶段的__结构内

PHP Notice: Undefined variable: fOT in C:'Program Files (x86)'Zend'Apache2'htdocs'Proj11'1.php on line 17

是脚本顶部的$phase->stepFunction($fOT);行。。。。它被定义了,所以不知道为什么会出现错误。。。。

我不知道为什么什么都不做,我很感激的帮助

谢谢你们!

            if (empty ($_GET['fot']) ) { 
                    $fOT = 'false'; 
                    } elseif ($_GET['true']) { $fOT = 'true'; }
            $phase = $_GET['phase'];        
            if(empty ($phase)){
                $phase = new phase1();
                $phase->start();
                } elseif ($phase = 1) {
                    $phase = new phase2();
                $phase->stepFunction($fOT, $_GET['step']);
                    }
                 class phase 1 { ... }
                    class phase2 {
        function __construct () {
        $dbFile = 'dbconfig.php';
        $this->dbFile = $dbFile;
        include_once ("$this->dbFile"); 

        $step = $_GET["step"];
        $username = $DB_USER;
        $password = $DB_PASS;
        $server = $DB_SERVER;
        $dbName = $DB_NAME;
        $this->step = $step;
        $this->dbFile = $dbFile;
        $this->username = $username;
        $this->password = $password;
        $this->server = $server;
        $this->dbName = $dbName;
        $db = new PDO ('mysql:host=' .$server.';dbname='.$this->dbName,$this->username,$this->password);
        $this->db = $db;
        if (empty ($_GET['fot']) ) { 
        $fOT = 'false'; 
        } elseif ($_GET['true']) { $fOT = 'true'; }
    $this->IDB = $this->handleDatabase( 1 );
    $this->IDB2 = $this->handleDatabase( 2);
    $this->IDB3 = $this->handleDatabase( 3);
        }

public function handleDatabase ($num = 1){
// Prepare SQL Statements
    $IDB1 = $this->db->prepare( 
         "CREATE TABLE pages (
          id int(11) NOT NULL auto_increment,
         subject_id int(11) NOT NULL,
          menu_name varchar(30) NOT NULL,
          position int(3) NOT NULL,
          visible tinyint(1) NOT NULL,
          content text NOT NULL,
          PRIMARY KEY  (id)
    )ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8");
    $IDB2 = $this->db->prepare("
        CREATE TABLE subjects (
          id int(11) NOT NULL auto_increment,
          menu_name varchar(30) NOT NULL,
          position int(3) NOT NULL,
          visible tinyint(1) NOT NULL,
          PRIMARY KEY  (id)
    )ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");
    $IDB3 = $this->db->prepare("
        CREATE TABLE users (
          id int(11) NOT NULL auto_increment,
          username varchar(50) NOT NULL,
          hashed_password varchar(40) NOT NULL,
          PRIMARY KEY  (id)
    )ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8");
    $name = "IDB".$num;
return isset( $$name)?$$name:false;
}
//Set Option to True or False
function createTablePages ($fOT){
    $r1 = $this->db->query('SHOW TABLES LIKE ''page''');
    if (count($r1->fetchAll()) > 0 && $fOT == 'false') {
        echo "The table PAGE exists";
    } elseif ($fOT == 'true') {
        $this->IDB->execute();
        echo "enteres";
                $this->stepFunction (1,false);
    }
}
function createTableSubjects ($fOT){
    $r2 = $this->db->query('SHOW TABLES LIKE ''subjects''');
    if (count($r2->fetchAll()) > 0  && $fOT == 'false') {
        echo "The table SUBJECTS exists ";
    } elseif ($fOT == 'true') {
        $this->IDB2->execute();
        $this->stepFunction (2,false);
    }
}
function createTableUsers ($fOT){
    $r3 = $this->db->query('SHOW TABLES LIKE ''users''');   
    if (count($r3->fetchAll()) > 0  && $fOT == 'false') {
        echo "The table USERS exists";
    } elseif ($fOT == 'true') {
        $this->IDB3->execute();
        echo "Would you like to populate all the tables?";
    }   
}

public function stepFunction ($fOT,$step){
switch ($step) {
    case 0: 
            $this->createTablePages ($fOT);
            break;
    case 1: 
            $this->createTableSubjects($fOT);
            break;
    case 2: $this->createTableUsers ($fOT);
            break;
    }

}
    }

不能在全局范围内使用方法中定义的变量。

改变这个,它将解决的一些错误

    $this->IDB = $this->handleDatabase()->$IDB;
    $this->IDB2 = $this->handleDatabase()->$IDB2;
    $this->IDB3 = $this->handleDatabase()->$IDB3;

    $this->IDB = $this->handleDatabase( 1 );
    $this->IDB2 = $this->handleDatabase( 2);
    $this->IDB3 = $this->handleDatabase( 3);

和的函数处理数据库

public function handleDatabase ( $num = 1){
// Prepare SQL Statements
$IDB1 = $this->db->prepare( 
     "CREATE TABLE pages (
      id int(11) NOT NULL auto_increment,
     subject_id int(11) NOT NULL,
      menu_name varchar(30) NOT NULL,
      position int(3) NOT NULL,
      visible tinyint(1) NOT NULL,
      content text NOT NULL,
      PRIMARY KEY  (id)
)ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8");
$IDB2 = $this->db->prepare("
    CREATE TABLE subjects (
      id int(11) NOT NULL auto_increment,
      menu_name varchar(30) NOT NULL,
      position int(3) NOT NULL,
      visible tinyint(1) NOT NULL,
      PRIMARY KEY  (id)
)ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");
$IDB3 = $this->db->prepare("
    CREATE TABLE users (
      id int(11) NOT NULL auto_increment,
      username varchar(50) NOT NULL,
      hashed_password varchar(40) NOT NULL,
      PRIMARY KEY  (id)
)ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8");
$name = "IDB".$num;
return isset( $$name)?$$name:false;
}

或者直接在方法中设置$this->IDB:

public function handleDatabase ( ){
// Prepare SQL Statements
$this->IDB  = $this->db->prepare( [...]

还有一些其他问题

 $this->IDB3->execute;

什么都不做,改为:

 $this->IDB3->execute();
相关文章: