Hi.
I have been trying to create a script that updates tables in mysql. In this instance,
I have been attempting to create a content management system. so i can update it in the web site itself. It is quite primitive at the moment. I have been trying it out by building a form which will update the navigation system. When you click on a page in the navigation system it takes you to the edit page.
But it just wont update. For instance if you change something in the text box it just puts it back again It just renews the page again. Can anyone see where I am going wrong? It is driving me bonkers...
The main page is as follows. I have put the important script for selected pages after it as it is on a seperate page:
<?php require_once("includes/connection.php");?>
<?php require_once("includes/functions.php");?>
<?php
// Validation
if (intval($_GET['subj']) == 0){
redirect_to("content.php");
}
if (isset($_POST['submit'])) { //if the page has been submittedsubmit is the submit button
$errors = array();
$required_fields = array('menu_name', 'position', 'visible');
foreach($required_fields as $fieldname){
if (!isset($_POST[$fieldname]) || empty($_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)){
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST["menu_name"]);
$position = mysql_prep($_POST["position"]);
$visible = mysql_prep($_POST["visible"]);
//query
$sql = "UPDATE subjects SET
menu_name='{$menu_name}',
position={$position},
visible={$visible}
WHERE id = {$id}";
// execute query
$result = mysqli_query($db, $sql);
// test to see if the update worked
}else{
// errors occured
$message = "There were " .count($errors). " in the form";
}
} // end of issset post
?>
<?php find_selected_subjects_and_pages();?>
<?php include("includes/header.php");?>
<div id="menu">
<?php navigation($sel_subject, $sel_page);?>
</div>
<div id="mainarea">
<div id="contentind"><h2>Edit Subject Page: <?php echo $sel_subject['menu_name'];?></h2>
<?php if (!empty($message)){
echo "<p>".message."</p>";} ?>
<form action="edit.php?subj=<?php echo urlencode( $sel_subject['id']);?>" method="post" >
<p>Subject Name: <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name'];?>" id="menu_name" /></p>
<p>Position: <select name="position">
<?php $subject_set = get_all_subjects();
$subject_count = mysqli_num_rows($subject_set);
for($count=1; $count <= $subject_count+1; $count++){
echo "<option value=\"{$count}\"";
if($sel_subject['position'] == $count) {
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>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
</p>
<input type="submit" value="Edit Subject" />
</form>
<br/>
The functions find_selected_subjects_and_pages:
function find_selected_subjects_and_pages(){
global $sel_subject;
global $sel_page;
if (isset($_GET['subj'])){
$sel_subject = get_subjects_by_id($_GET['subj']);
$sel_page = NULL;
}elseif (isset($_GET['page'])){
$sel_subj = NULL;
$sel_page =get_page_by_id( $_GET['page']);
}else{
$sel_page = NULL;
$sel_subj = NULL;
}
}
Your time is very much appreciated,
cass27