Database Connection Failure!";
}
else // If the database connects successfully continue
{
echo "
Create Project
";
echo "
Track by User Version
";
if(isset($_POST['projectName'])) // Checks to see if the form has been submitted
{
// Stores all values and errors from the form into an array
$formValues = $_POST;
$formErrors = array();
if (!validateEnterProjectForm($formValues, $formErrors)) // Conducts error checking
{
showEnterProjectForm ($formValues, $formErrors);
showTableList($DB_NAME);
}
else
{
// Santizes and intializes project name
$tableName = mysql_real_escape_string($_POST['projectName']);
$queryFindTable="SELECT * FROM `$tableName`";
$resultFindTable=@mysql_query($queryFindTable);
if (!$resultFindTable) // If the project does not exist, create the table
{
mysql_query("CREATE TABLE `$tableName`(
`userID` int(10) unsigned NOT NULL auto_increment,
`dateAdded` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`userID`))");
echo $tableName . " Successfully created!";
}
else // The table already exists, print an error
{
echo "Table " . $tableName . " already exists!";
}
}
}
else // If the form has never been submitted, show the initial starting state of the page
{
showEnterProjectForm (null, null);
showTableList($DB_NAME);
}
// mysql_close(); // Closes all open DB connections
} // End of database successfully connected
///// CREATE PROJECT FUNCTION DEFINITIONS ///////
/////////////////////////////
// Function showTableList
// Description: Queries a database that is passed in and displays a list of all of the tables found in the database.
// Arugments: $dbName - Takes in the database name and uses it for the query
/////////////////////////////
function showTableList($dbName)
{
$queryListTables="SHOW TABLES FROM $dbName";
$resultListTables = mysql_query($queryListTables);
if (mysql_num_rows($resultListTables) <= 0) // Checks first to see if there are any tables in the database. If not it doesn't print anything.
{
echo "
No Existing Projects
";
}
else
{
echo "
Existing Projects
";
echo "\n\n
";
echo "\n";
echo "\n\t| Project Name | ";
$rowColorNum = 1;
while ($row = mysql_fetch_row($resultListTables)) // Prints a list of all the tables in the database
{
if ( ($rowColorNum % 2) == 0 ) // Alternates row colors
{
$rowColor = 'even';
}
else
{
$rowColor = 'odd';
}
echo "\n
\n";
echo "\n| ";
echo $row[0];
echo "\n | ";
echo "\n
";
$rowColorNum++;
}
echo "
";
}
}
/////////////////////////////
// Function showEnterProjectForm
// Description: Displays the form to the page so that the user can enter in a Project name.
// Arugments: $values - Takes in the previous values (if any) from the last submission of the form.
// $errors - Takes in the previous errors (if any) from the last submission of the form.
/////////////////////////////
function showEnterProjectForm($values, $errors)
{
echo "
\n";
}
/////////////////////////////
// Function validaeEnterProjectForm
// Description: Checks the text field for any possible incorrect input (error checking)
// Arugments: &$values - Takes in the previous values (if any) from the last submission of the form (Passed by reference).
// &$errors - Takes in the previous errors (if any) from the last submission of the form (Passed by reference).
/////////////////////////////
function validateEnterProjectForm(&$values, &$errors)
{
// This checks to see if the project name is blank or if it contains any unwanted characters.
if (trim($values['projectName']) == "")
$errors['projectName'] = ' * Please enter in your project name';
// Checks to see if there are any unwanted characters
elseif (preg_match("([~#$%@!?<>+_^*=:{}()[\]|\;/]|[|])", stripcslashes($values['projectName'])))
$errors['projectName'] = ' * Please do not use special characters like ^*~;{}';
// Checks to see if the project name is at least 2 characters long
elseif (!preg_match("/[-'a-zA-Z]{2,}/", $values['projectName']) )
$errors['projectName'] = ' * Project name must be at least 2 characters long';
// This returns a boolean expression that compares the number of errors to zero and returns it value to the function call
// Ff false then the form is run again and the errors are displayed
return (count($errors) == 0);
}
///// CREATE PROJECT FUNCTION DEFINITIONS ///////
?>