PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/learning-php/basics/cms/edit-subject.php

https://github.com/warrentang/learning-php
PHP | 97 lines | 89 code | 7 blank | 1 comment | 19 complexity | a3877531959be911bf0952dba27a80a9 MD5 | raw file
  1. <?php require_once("header.php"); ?>
  2. <?php
  3. if(intval($_GET['subj']) == 0) {
  4. redirect_to("content.php");
  5. }
  6. if(isset($_POST['submit'])) {
  7. $errors = array();
  8. $required_fields = array('menu-name', 'position', 'visible');
  9. foreach($required_fields as $field_name)
  10. if(!isset($_POST[$field_name]) || !check_string($_POST[$field_name])) {
  11. $errors[] = "empty field: {$field_name}";
  12. }
  13. $fields_with_lengths = array('menu-name' => 30);
  14. foreach ($fields_with_lengths as $field_name => $value) {
  15. if($_POST[$field_name] > $value) {
  16. $errors[] = "length exceeded: {$field_name}";
  17. }
  18. }
  19. if(empty($errors)) {
  20. $id = mysql_prepare($_GET['subj']);
  21. $menu_name = mysql_prepare($_POST["menu-name"]);
  22. $position = mysql_prepare($_POST["position"]);
  23. $visible = mysql_prepare($_POST["visible"]);
  24. $query = "update subjects set
  25. menu_name = '{$menu_name}',
  26. position = {$position},
  27. visible = {$visible}
  28. where id = {$id}";
  29. $result = mysql_query($query);
  30. if(mysql_affected_rows() == 1) {
  31. $message = "Update successful.";
  32. } else {
  33. $message = "Update faild: ";
  34. $message .= $query . "<br />";
  35. $message .= mysql_error();
  36. }
  37. } else {
  38. //$errors not empty
  39. $message = implode("<br />", $errors);
  40. }
  41. }
  42. ?>
  43. <?php get_selected_page(); ?>
  44. <nav id="nav">
  45. <h2>Navigation</h2>
  46. <?php echo navigation($sel_subject, $sel_page); ?>
  47. </nav>
  48. <section id="main">
  49. <h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2>
  50. <?php if(isset($message)) { ?>
  51. <div id="message"><?php echo $message; ?></div>
  52. <?php } ?>
  53. <form action="edit-subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post">
  54. <p>
  55. <label for="menu-name">Subject name: </label>
  56. <input type="text" name="menu-name" id="menu-name" value="<?php echo $sel_subject['menu_name']; ?>" />
  57. </p>
  58. <p>
  59. <label for="position">Position:</label>
  60. <select name="position" id="position">
  61. <?php
  62. $subject_set = get_all_subjects();
  63. $subject_count = mysql_num_rows($subject_set);
  64. for($count = 1; $count <= $subject_count + 1; $count++) {
  65. echo "<option value=\"{$count}\"";
  66. if($count == $sel_subject['position']) {
  67. echo " selected";
  68. }
  69. echo ">{$count}</option>";
  70. }
  71. ?>
  72. </select>
  73. </p>
  74. <p>
  75. <label>Visible:</label>
  76. <input type="radio" name="visible" id="visible-yes"
  77. <?php if($sel_subject['visible'] == 1) { echo "checked"; } ?> value="1"><label for="visible-yes">Yes</label>
  78. <input type="radio" name="visible" id="visible-no"
  79. <?php if($sel_subject['visible'] == 0) { echo "checked"; } ?> value="0"><label for="visible-no">No</label>
  80. </p>
  81. <p><input type="submit" name="submit" value="Submit" />
  82. </form>
  83. <menu>
  84. <a href="content.php">Cancel</a> |
  85. <a href="delete-subject.php?subj=<?php echo urlencode($sel_subject["id"]); ?>"
  86. onclick="return confirm('Are you sure to delete the subject?'); ">Delete</a> |
  87. <a href="edit-page.php?action=new&amp;subj=<?php echo urlencode($sel_subject["id"]); ?>">+ Add a new page</a>
  88. </menu>
  89. </section>
  90. <?php require_once("footer.php"); ?>