zzz+special+final+subject+CRUD

edit_subject.php (before adding messages)

code    30); foreach($fields_with_lengths as $fieldname => $maxlength ) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } }

if (empty($errors)) { // perform update $id = mysql_prep($_GET['subj']); $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']);

$query = "UPDATE tblsubjects SET                   menu_name = '{$menu_name}',                    position = {$position},                    visible = {$visible}                WHERE id = {$id}";

$result = mysql_query($query,$connection);

if(mysql_affected_rows == 1) { // Success } else { // Falled }

} else { // errors occured }

} // end of if(isset($_POST['submit')) ?>  



Edit Subject: 

" method="post"> Subject name: " id="menu_name" /> Position: {$count} "; }           ?>    Visible:  /> No        /> Yes 

Cancel



code

edit_subject.php (with messages)

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php

if(intval($_GET['subj']) == 0) { // if subj is not a valid integer go back to content.php redirect_to("content.php"); }

if(isset($_POST['submit'])) {

$errors = array;

// Form Validation $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) { $errors[] = $fieldname; }   }

$fields_with_lengths = array('menu_name' => 30); foreach($fields_with_lengths as $fieldname => $maxlength ) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } }

if (empty($errors)) { // perform update $id = mysql_prep($_GET['subj']); $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']);

$query = "UPDATE tblsubjects SET                   menu_name = '{$menu_name}',                    position = {$position},                    visible = {$visible}                WHERE id = {$id}";

$result = mysql_query($query,$connection);

if(mysql_affected_rows == 1) { // Success $message = "The subject was updated successfully."; } else { // Falled $message = "The subject update failed."; $message .= " ". mysql_error; }

} else { // form validation errors occured $message = "There were ". count($errors). " errors in the form."; }

} // end of if(isset($_POST['submit')) ?> <?php    find_selected_page; // note if this is above the update stuff, the value in the form field will not update ?> <?php include("includes/header.php"); ?>

<?php echo navigation($sel_subject, $sel_page); ?>

Edit Subject: <?php echo $sel_subject['menu_name']; ?> <?php if(!empty($message)) { echo "<p class=\"message\">". $message. " "; } ?> <?php // list of errors if(!empty($errors)) { echo "<p class=\"errors\">"; echo "Please check the following fields: "; foreach($errors as $error) { echo " - ". $error. " ";   }    echo " "; } ?>

<form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post"> Subject name: <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" /> Position: <?php $subject_set = get_all_subjects; $subject_count = mysql_num_rows($subject_set); // $subject_count + 1 b/c we are adding a subject for($count=1; $count <= $subject_count+1; $count++) { echo "<option value=\"{$count}\""; if($sel_subject['position'] == $count) { echo " selected"; } // do not forget the space before selected echo ">{$count} "; }           ?>    Visible: <input type="radio" name="visible" value="0"<?php if($sel_subject['visible'] == 0) { echo " checked"; } ?> /> No       <input type="radio" name="visible" value="1"<?php if($sel_subject['visible'] == 1) { echo " checked"; } ?> /> Yes <input type="submit" name="submit" value="Edit Subject" />

Cancel</a>

<?php require("includes/footer.php"); ?>

code

Add link to delete the subject after the submit button

code

" onclick="return confirm('Are you sure?');">Delete this Subject</a> code

delete_subject.php

code <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php

if(intval($_GET['subj']) == 0) { // if subj is not a valid integer go back to content.php redirect_to("content.php"); } $id = mysql_prep($_GET['subj']); if($subject = get_subject_by_id($id)) {

$query = "DELETE FROM tblsubjects WHERE id = {$id} LIMIT 1"; $result = mysql_query($query,$connection); if(mysql_affected_rows == 1) { // Success redirect_to("content.php"); } else { // Failed $message = " The subject delete failed. "; $message .= " ". mysql_error. " ";			$message .= " Return to Main Page</a> "; echo $message; } } else { // subject did not exist $message = " The subject did not exist. "; $message .= " Return to Main Page</a> "; echo $message; // can also use redirect_to("content.php"); }

?> <?php mysql_close($connection); ?> code

.