beginTransaction(); $sth = $dbh->prepare("SELECT * FROM courses"); $count = 0; if ($sth->execute()) { while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { if ($row['id'] === $_POST['id']) { $insert = FALSE; } } //echo "here"; if ($insert) { $sth = $dbh->prepare("INSERT INTO courses (id, name, description) values (?, ?, ?)"); $sth->execute(array($_POST['id'], $_POST['name'], $_POST['description'])); } else { $dbh->beginTransaction(); $sth = $dbh->prepare("UPDATE courses SET name = ?, description = ? WHERE id = ?"); $sth->execute(array($_POST['name'], $_POST['description'], $_POST['id'])); //print_r($sth->errorInfo()); $dbh->commit(); } } } catch (Exception $e) { print ("Fail! " . $e->getMessage() . "\n"); } } $root = $doc->appendChild($root); $root->appendChild(generateHead($doc)); $body = $doc->createElement('body'); $root->appendChild($body); //$body->appendChild(generateMastHead($doc, $baseDir)); //$body->appendChild($doc->createElement('hr')); /* $body->appendChild(generateMemberNavigationBar($doc)); if (returnInstructorStatus($_SESSION['MemberID'])) if ($_SESSION['sub_nav'] === "Admin") $body->appendChild(generateAdminNavigationBar($doc)); else if ($_SESSION['sub_nav'] === "Director") $body->appendChild(generateDirectorNavigationBar($doc)); else $body->appendChild(generateTrainingNavigationBar($doc)); else if (returnAdminStatus($_SESSION['memberID'])) $body->appendChild(generateAdminNavigationBar($doc)); */ /* * do not set session sub nav */ //$body->appendChild(generateSelectedMenuBar($doc)); $body->appendChild(generateTopStickyDiv($doc, $baseDir)); /* * Insert content here. */ $bodyDiv = $doc->createElement('div'); $bodyDiv->setAttribute('class', 'BodyDiv'); /* $tableDiv = $doc->createElement('div'); //$tableDiv->setAttribute('display', 'block'); $tableDiv->setAttribute('class', 'FloatLeft50Div'); $table = $doc->createElement('table'); //$table.setAttribute('class' //$table->setAttribute('class', 'FloatLeft50Div'); $thead = $doc->createElement('thead'); $tr = $doc->createElement('tr'); $th = $doc->createElement('th'); $a = $doc->createElement('a'); $a->appendChild($doc->createTextNode('ID')); $th->appendChild($a); $tr->appendChild($th); $th = $doc->createElement('th'); $a = $doc->createElement('a'); $a->appendChild($doc->createTextNode('Course Name')); $th->appendChild($a); $tr->appendChild($th); $th = $doc->createElement('th'); $a = $doc->createElement('a'); $a->appendChild($doc->createTextNode('Description')); $th->appendChild($a); $tr->appendChild($th); $thead->appendChild($tr); $table->appendChild($thead); $tbody = $doc->createElement('tbody'); $tint = false; $edit = NULL; $highestID = 0; try { //print_r(PDO::getAvailableDrivers()); $dbh = new PDO($dsn, $dbUserName, $dbPassWord, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); $dbh->beginTransaction(); $sth = $dbh->prepare("SELECT * FROM courses"); $count = 0; if ($sth->execute()) { while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { if (isset($_GET['id'])) if ($row['id'] === $_GET['id']) $edit = $row; $count++; if ($row['id'] > $highestID) $highestID = $row['id']; $tr = $doc->createElement('tr'); if ($tint === true) { $tr->setAttribute('class', 'AlternateRow'); } $td = $doc->createElement('td'); $rr = $doc->createElement('a'); $rr->setAttribute('href', "courseEditor.php?id=${row['id']}"); $rr->appendChild($doc->createTextNode($row['id'])); $td->appendChild($rr); $tr->appendChild($td); $td = $doc->createElement('td'); $td->appendChild($doc->createTextNode($row['name'])); $tr->appendChild($td); $td = $doc->createElement('td'); $td->appendChild($doc->createTextNode($row['description'])); $tr->appendChild($td); $tbody->appendChild($tr); } } $table->appendChild($tbody); $tableDiv->appendChild($table); } catch (Exception $e) { echo "Failed!" . $e->getMessage() . "\n"; }*/ if (isset($_GET['id'])) $edit = getCourseRowByID($_GET['id']); else $edit = NULL; $highestID = getHighestCourseID(); $bodyDiv->appendChild(generateCourseTable($doc)); if (!($edit === NULL)) { $editorDiv = $doc->createElement('div'); $editorDiv->setAttribute('id', 'CourseEditorDiv'); $form = $doc->createElement('form'); $form->setAttribute('action', 'courseEditor.php'); $form->setAttribute('method', 'post'); $label = $doc->createElement('H3'); $label->appendChild($doc->createTextNode("Course ID: " . $edit['id'])); $form->appendChild($label); $input = $doc->createElement('input'); $input->setAttribute('type', 'hidden'); $input->setAttribute('name', 'id'); $input->setAttribute('readonly', 'readonly'); $input->setAttribute('value', $edit['id']); $form->appendChild($input); $fieldSet = $doc->createElement('fieldset'); //$fieldSet->setAttribute('style', 'width:200px; max-width:300px;'); //margin-top:0.5em;'); $fieldSetDiv = $doc->createElement('div'); $label = $doc->createElement('label', 'Course Name:'); $label->setAttribute('for', 'name'); $label->setAttribute('class', 'CourseEditorInputLabel'); $fieldSetDiv->appendChild($label); $input = $doc->createElement('input'); $input->setAttribute('type', 'text'); $input->setAttribute('name', 'name'); $input->setAttribute('autocomplete', 'off'); $input->setAttribute('required', 'required'); if (isset($edit['name'])) $input->setAttribute('value', $edit['name']); $fieldSetDiv->appendChild($input); $label = $doc->createElement('br'); $fieldSetDiv->appendChild($label); /* $label = $doc->createElement('label', 'Description:'); $label->setAttribute('for', 'description'); $label->setAttribute('class', 'CourseEditorInputLabel'); $fieldSetDiv->appendChild($label); $input = $doc->createElement('input'); $input->setAttribute('type', 'text'); $input->setAttribute('name', 'description'); $input->setAttribute('autocomplete', 'off'); $input->setAttribute('required', 'required'); if (isset($edit['description'])) $input->setAttribute('value', $edit['description']); $fieldSetDiv->appendChild($input); $label = $doc->createElement('br'); $fieldSetDiv->appendChild($label); */ $label = $doc->createElement('label', 'Description:'); $label->setAttribute('for', 'description'); $label->setAttribute('class', 'CourseEditorInputLabel'); $fieldSetDiv->appendChild($label); $input = $doc->createElement('textarea'); $input->setAttribute('type', 'text'); $input->setAttribute('name', 'description'); $input->setAttribute('autocomplete', 'off'); $input->setAttribute('rows', '6'); $input->setAttribute('required', 'required'); if (isset($edit['description'])) $input->appendChild($doc->createTextNode($edit['description'])); $fieldSetDiv->appendChild($input); $label = $doc->createElement('br'); $fieldSetDiv->appendChild($label); $fieldSet->appendChild($fieldSetDiv); $form->appendChild($fieldSet); $input = $doc->createElement('input'); $input->setAttribute('type', 'reset'); $form->appendChild($input); $input = $doc->createElement('input'); $input->setAttribute('type', 'submit'); $form->appendChild($input); $editorDiv->appendChild($form); $bodyDiv->appendChild($editorDiv); } else { $nextHighestID = $highestID + 1; $editorDiv = $doc->createElement('div'); $editorDiv->setAttribute('id', 'CourseEditorDiv'); $editorDiv->setAttribute('class', 'sticky'); $form = $doc->createElement('form'); $form->setAttribute('action', 'courseEditor.php'); $form->setAttribute('method', 'post'); $input = $doc->createElement('input'); $input->setAttribute('type', 'hidden'); $input->setAttribute('value', 'Add'); $input->setAttribute('name', 'action'); $input->setAttribute('readonly', 'readonly'); $form->appendChild($input); $label = $doc->createElement('H3'); $label->appendChild($doc->createTextNode("Add New Course")); $form->appendChild($label); $fieldSet = $doc->createElement('fieldset'); //$fieldSet->setAttribute('style', 'width:200px; max-width:300px;'); //margin-top:0.5em;'); $fieldSetDiv = $doc->createElement('div'); $fieldSet->appendChild($fieldSetDiv); $label = $doc->createElement('label', 'Course ID:'); $label->setAttribute('for', 'id$'); $label->setAttribute('class', 'CourseEditorInputLabel'); $fieldSetDiv->appendChild($label); $input = $doc->createElement('input'); $input->setAttribute('type', 'number'); $input->setAttribute('name', 'id'); $input->setAttribute('autocomplete', 'off'); $input->setAttribute('required', 'required'); $input->setAttribute('value', "$nextHighestID"); $input->setAttribute('min', "$nextHighestID"); $input->setAttribute('step', '1'); $fieldSetDiv->appendChild($input); $label = $doc->createElement('label', 'Course Name:'); $label->setAttribute('for', 'name'); $label->setAttribute('class', 'CourseEditorInputLabel'); $fieldSetDiv->appendChild($label); $input = $doc->createElement('input'); $input->setAttribute('type', 'text'); $input->setAttribute('name', 'name'); $input->setAttribute('autocomplete', 'off'); $input->setAttribute('required', 'required'); $fieldSetDiv->appendChild($input); $label = $doc->createElement('br'); $fieldSetDiv->appendChild($label); /* $label = $doc->createElement('label', 'Description:'); $label->setAttribute('for', 'description'); $label->setAttribute('class', 'CourseEditorInputLabel'); $fieldSetDiv->appendChild($label); $input = $doc->createElement('input'); $input->setAttribute('type', 'text'); $input->setAttribute('name', 'description'); $input->setAttribute('autocomplete', 'off'); $input->setAttribute('required', 'required'); $fieldSetDiv->appendChild($input); $label = $doc->createElement('br'); $fieldSetDiv->appendChild($label); */ $label = $doc->createElement('label', 'Description:'); $label->setAttribute('for', 'description'); $label->setAttribute('class', 'CourseEditorInputLabel'); $fieldSetDiv->appendChild($label); $input = $doc->createElement('textarea'); $input->setAttribute('type', 'text'); $input->setAttribute('name', 'description'); $input->setAttribute('autocomplete', 'off'); $input->setAttribute('rows', '6'); $input->setAttribute('required', 'required'); $input->appendChild($doc->createTextNode("")); /* a child node is needed to close the textarea */ $fieldSetDiv->appendChild($input); $label = $doc->createElement('br'); $fieldSetDiv->appendChild($label); $form->appendChild($fieldSet); $input = $doc->createElement('input'); $input->setAttribute('type', 'reset'); $form->appendChild($input); $input = $doc->createElement('input'); $input->setAttribute('type', 'submit'); $form->appendChild($input); $editorDiv->appendChild($form); //$bodyDiv->appendChild($editorDiv); $body->appendChild($editorDiv); // Needs to be in the body for sticky } $body->appendChild($bodyDiv); $body->appendChild(generateFooter($doc)); if ($prettyPretty) $doc->formatOutput = true; echo $doc->saveXML(); } else generateIndexRedirect(); ?>