<?php
$page_title = "Add Course | CES Admin";
include_once($_SERVER['DOCUMENT_ROOT'] . "/php/common_functions.php"); //common functions in the course registartion system
include_once($_SERVER['DOCUMENT_ROOT'] . "/php/config.php"); //holds global config variables
check_if_authenticated('admin');//check if user is logged in
if(isset($_POST['cancel']) || isset($_POST['back'])){ //form has been canceled
header('location:index.php');
exit;
}//if
if(isset($_POST['submit'])){
//form has been submitted, don't prompt for info; add form data to database and show what has been added
$db = mysqli_connect($mysql_server, $user, $pass, $database);
$course_name = htmlentities($_POST['course_name'], ENT_QUOTES);
$course_description = htmlentities($_POST['course_description'], ENT_QUOTES);
$sql = "INSERT INTO ces_courses (course_name, course_description, level_id) VALUES (\"$course_name\", \"$course_description\", \"$_POST[level_id]\")";
if(mysqli_query($db, $sql)){ //if the update is successful show the new entry
$last_id = mysqli_insert_id($db);
header("location:index.php#".$last_id);
exit;
} //end if
else {//else the update did not happen so output an error
$db_error = print_sql_error('Error - course submission failed when doing:', $sql);
}//else
}
include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/php/top-admin.php"); //get the HTML heading common to all pages in the CES admin module
?>
<article class="ces-admin">
<h2>CES Admin - Add Course</h2>
<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/php/nav-admin.php");
//opens connection to database
$db = mysqli_connect($mysql_server, $user, $pass, $database);
if (!isset($_POST['submit'])){ //if form has not been submitted yet
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<div class="form-group">
<label for="course_name">Course Name:</label>
<input class="form-control" type='text' id='course_name' name='course_name' maxlength='200' placeholder="Course Name" autofocus required>
</div>
<div class="form-group">
<label for="course_description">Course Description:</label>
<textarea class="form-control" rows='8' style="height:auto; resize:none;" name='course_description' wrap='soft' required placeholder="Course Description"></textarea>
</div>
<div class="form-group">
<label for="level_description">Course Level:</label>
<?php
//generate select list of course level IDs
$level_sql = mysqli_query($db, "SELECT level_id, level_description FROM ces_levels ORDER BY level_id");
build_db_select_list($level_sql, 'level_id', '', 'level_description', 'class="form-control" style="max-width:100%"');
?>
</div>
<input type='submit' name='submit' value="Add Course" class="btn btn-lg btn-block ces-green-btn">
<input type='reset' name='reset' value="Reset Form" class="btn btn-lg btn-block ces-grey-btn">
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type='submit' name='cancel' value="Cancel" class="btn btn-lg btn-block ces-orange-btn">
</form>
<?php
} //end if
?>
</article>
</section><!-- #middle-->
</div><!-- #wrapper -->
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/require/footer.php';?>