Basically I have a piece of code that has a switch statement and some functions. It goes to the switch statement using a parameter given by the browser “?step=X” and then selects the appropriate function.
My problem is even when at the end of each function I specify to go to the next switch statement it never does it and somehow it becomes an infinite loop of the function that I selected with the “step=x” in the browser…
Why is getting stuck in 1 function and not iterating through them? (the code for the step is highlighted at the end of the script)
I get the output in the browser of any selected function. so It enters the switch and the selected function… but then it becomes and infinite loop because in the broswer i get 300 of the same echo statements. It is never able to iterate through them or exit the selected function.
<?php
//DB Config File
$dbFile = 'dbconfig.php';
$username = $_GET['username'];
$password = $_GET['password'];
$server = $_GET['server'];
$dbname = $_GET['dbname'];
$step = $_GET["step"];
function createfile ($dbFile) {
//Creates File and populates it.
$fOpen = fopen($dbFile, 'w');
global $username, $password, $server, $dbname;
$fString .= "<?php\n";
$fString .= "// Database Constants\n";
$fString .= "\$DB_SERVER =" . "\"" . $server . "\";\n";
$fString .= "\$DB_USER =" . "\"" . $username . "\";\n";
$fString .= "\$DB_PASS =" . "\"" . $password . "\";\n";
$fString .= "\$DB_NAME =". "\"" . $dbname . "\";\n";
$fString .= "?>";
fwrite($fOpen, $fString);
fclose($fOpen);
return true;
}
try {
$db = new PDO ('mysql:host=' .$server.';dbname='.$dbname,$username,$password);
if ($db) { //if succesful at connecting to the DB
if (file_exists($dbFile)){
if (is_readable($dbFile) && is_writable($dbFile)){
//Creates File, populates it and redirects the user
if (createfile($dbFile)) {
echo "nelxt";
stepFunction($step);
exit ();
}
} else {
echo "The file {$dbFile} cannot be accessed. Please configure the file manualy or grant Write and Read permission."; }
} else {
//Creates File, populates it and redirects the user
if (createfile($dbFile)) {
echo "next";
stepFunction($step);
exit ();
}
}
}
} catch (PDOException $e) { //Catchs error if can't connect to the db.
echo 'Connection failed: ' . $e->getMessage();
}
// Prepare SQL Statements
$IDB = $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 = $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 = $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");
//Set Option to True or False
if (empty ($_GET['fot']) ) {
$fOT = false;
} else { $fOT = true;
}
///////////////////////////////
// PROBLEMATIC STEP BEGINS HERE
///////////////////////////////
function createTablePages (){
global $db,$IDB;
echo "0 <br>";
stepFunction (1);
}
function createTableSubjects ($fOT){
global $db,$IDB2;
echo "1 <br>";
stepFunction (2);
}
function createTableUsers ($fOT){
global $db,$IDB3;
echo "3 <br>";
}
function stepFunction ($step,$fOT){
global $db,$IDB1,$IDB2,$step,$fOT;
switch ($step) {
case 0: echo "hola";
createTablePages ($fOT);
break;
case 1: echo "hola2";
createTableSubjects($fOT);
break;
case 2: createTableUsers ($fOT);
break;
}
}
?>
Fix
to
You are overriding your
$stepvariable with the global one of the same name. Thus when you come with?step=0you callstepFunctionwith0then it goes tocreateTablePagesthen it goes tostepFunctionwith1, but is immediately replaced with 0 (because that’s what the global$step‘s value is) and again and again…