0

Hi people..
I got a problem which i cannot delete a a subject from my database through a page that i created.

I've made a page called edit_subject.php where there is a form which i could then delete/edit any subjects that i want.and this is the code :

<?php require_once("includes/functions.php");
?>
<?php
$connection = mysql_connect("localhost","root","mypassword");
if (!$connection) {
  die ("database failed to connect " . mysql_error());
}

$db_select = mysql_select_db("newcompany");
if (!$db_select) {
  die ("database failed to connect " . mysql_error());
}
?>
<?php
// naming a new function as a data query (see 'step 3' of mysql database)
/*remember that every time we wanna want to take somthg frm the database
  we have to perform the steps in CRUD */
function get_subject_by_id($subject_id) {
  $result_set = mysql_query("SELECT * FROM subjects WHERE id=$subject_id");
     if (!$result_set) {
         die("Database query failed: " . mysql_error());
        }
// this is the 'use data step' (see 'step 4' of mysql database)
// this is used to echo the data (or could say 'use the data')
  	if ($subject = mysql_fetch_array($result_set)) {
			return $subject;
 	}
}
?>
<?php
// naming a new function as a data query (see 'step 3' of mysql database)
/*remember that every time we wanna want to take somthg frm the database
  we have to perform the steps in CRUD */
function get_page_by_id($page_id) {
  $result_set = mysql_query("SELECT * FROM pages WHERE id=$page_id");
     if (!$result_set) {
         die("Database query failed: " . mysql_error());
        }
// this is the 'use data step' (see 'step 4' of mysql database)
// this is used to echo the data (or could say 'use the data')
  	if ($page = mysql_fetch_array($result_set)) {
			return $page;
 	}
}
?>
<?php
//this is to see which "subject" that being selected.Then it is pass as the argument,
//then being echo at the content area,only the menu name.
//anyway,this is the way to appear the menu name that is being selected at the navigation.
	if (isset($_GET['subj'])) {
		$sel_subject = get_subject_by_id($_GET['subj']);
		$sel_page = NULL;
	} elseif (isset($_GET['page'])) {
		$sel_subject = NULL;
		$sel_page = get_page_by_id($_GET['page']);
	} else {
		$sel_subject = NULL;
		$sel_page = NULL;
	}
?>
<?php
               if (intval($_GET['subj']) == 0) {
			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])) {
					$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 = "UPDATE subjects 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 succesfully updated.";

                        } else {
                          // Failed
                          $message = "The subject update failed.";
                          $message .= "<br />" . mysql_error();
                        }


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

		} // end: if (isset($_POST['submit']))
?>
<head>
<meta http-equiv="Content-Language" content="en-us">
</head>

<table border="0" cellpadding="0" cellspacing="0" width="1055" height="566">
	<!-- MSTableType="layout" -->
	<tr>
		<td valign="top" colspan="2" height="77">
		<!-- MSCellType="ContentHead" -->
		<p align="center"><font face="Agency FB" size="7">newcompany</font></td>
	</tr>
	<tr>
		<td valign="top" rowspan="2" width="181">
		<!-- MSCellType="NavBody" -->

                <?php
		// 3. Perform database query
		$subject_set = mysql_query("SELECT * FROM subjects",$connection);
		if (!$subject_set) {
			die("Database query failed: " . mysql_error());
		}

		// 4. Use returned data
		while ($row = mysql_fetch_array($subject_set)) {

                  echo "<a href='edit_subject.php?subj={$row["id"]}'>{$row['menu_name']}</a>"."<br/>";

                   $page_set = mysql_query("SELECT * FROM pages WHERE subject_id={$row["id"]}");
                     if (!$page_set) {
                        die ("Database query failed: " . mysql_error());
                        }

                    while ($page= mysql_fetch_array($page_set)) {
                        echo "<a href='content.php?page={$page["id"]}'>{$page['menu_name']}</a>"."<br/>";
                        }

                  }

		?>


                <a href="new_subject.php">+ Add A new Subject</a>

		&nbsp;


                </td>
		<td valign="top" height="52">
		<!-- MSCellType="ContentHead2" -->
		&nbsp;</td>
	</tr>
	<tr>
		<td valign="top" height="437" width="874">
		<!-- MSCellType="ContentBody" -->
		&nbsp;

                <h2>Edit Subject: <?php echo $sel_subject['menu_name'];?></h2>
                <?php if (!empty($message)) {
                  echo "$message";}
                ?>
              	<?php
			// output a list of the fields that had errors
			if (!empty($errors)) {

				echo "Please review the following fields:<br />";
				foreach($errors as $error) {
					echo " - " . $error . "<br />";
				}

			}
                ?>
                <form action="edit_subject.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 = mysql_num_rows($subject_set);

                   //$subject_count+1 because we are adding a subject.
                   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" value="0" name="visible"
                <?php
                if ($sel_subject['visible'] == 0) {echo "checked";}
                ?> >No
                &nbsp;
                <input type="radio" value="1" name="visible"
                <?php
                 if ($sel_subject['visible'] == 1) {echo "checked";}
                ?> >Yes
                </p>
                <p><input type="submit" name="submit" value="Edit subject">

               	&nbsp;&nbsp;
               	<a href="delete_subject.php?subj=
                <?php $sel_subject['id']; ?>"
                >Delete Subject</a>
                </form>
                <br />
                <br />
                <a href="content.php">Cancel</a>
                <br />
                </td>
	</tr>
</table>
<?php
mysql_close($connection);
?>

and if user need to click on delete to delete the subject which then this edit_subject.php page will make a connection to the page delete_subject.php to delete the subject.This is the code from delete_page.php :

<?php require_once("includes/functions.php");
?>
<?php
$connection = mysql_connect("localhost","root","mypassword");
if (!$connection) {
  die ("database failed to connect " . mysql_error());
}

$db_select = mysql_select_db("newcompany");
if (!$db_select) {
  die ("database failed to connect " . mysql_error());
}
?>
<?php
// naming a new function as a data query (see 'step 3' of mysql database)
/*remember that every time we wanna want to take somthg frm the database
  we have to perform the steps in CRUD */
function get_subject_by_id($subject_id) {
  $result_set = mysql_query("SELECT * FROM subjects WHERE id=$subject_id");
     if (!$result_set) {
         die("Database query failed: " . mysql_error());
        }
// this is the 'use data step' (see 'step 4' of mysql database)
// this is used to echo the data (or could say 'use the data')
  	if ($subject = mysql_fetch_array($result_set)) {
			return $subject;
 	}
}
?>
<?php
        if (intval($_GET['subj']) == 0) {
		header("Location: content.php");
	        exit;
        }

	$id = mysql_prep($_GET['subj']);

	if ($subject = get_subject_by_id($id)) {

		$query = "DELETE FROM subjects WHERE id = {$id} LIMIT 1";
		$result = mysql_query($query, $connection);
		if (mysql_affected_rows() == 1) {
			header("Location: content.php");
		        exit;
                } else {
			// Deletion Failed
			echo "<p>Subject deletion failed.</p>";
			echo "<p>" . mysql_error() . "</p>";
			echo "<a href=\"content.php\">Return to Main Page</a>";
		}
	} else {
		// subject didn't exist in database
		header("Location: content.php");
                exit;
        }
?>
<?php
mysql_close($connection);
?>

but the problem is the subject is not deleted and it is only redirected me to the page of content.php which what stated in this code :

if (intval($_GET['subj']) == 0) {
		header("Location: content.php");
	        exit;
        }

i think that this part of code maybe the reason of the errors because when i change the location to any other page it will redirected me to that page and doing the deletion as what i wanted..


I hope somebody could help me out..

Sory if the code is long to be gone through..

Thank You for helping :)

Edited by samsons17: n/a

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by JRM
0

actually i also not sure what that intval() function is doing there..
i just follow the tutorial and that is the part i got from that..

Edited by samsons17: n/a

0

there is a problem in your line no 217-218. $sel_subject; is not having proper value.

<a href="delete_subject.php?subj=                
<?php $sel_subject['id']; ?>"
0

rajarajan07 : em,i learn the video tutorial that i downloaded from internet.

urtrivedi : i've been using that $sel_subject variable to do the other stuff also and it works fine for me..Why could this time it's value is inappropriate?

0

set your php.ini to display_errors = on
this should help define the problem.

also, my favorite thing is to insert a "detector" to show the variable state.

var_dump($sel_subject['id']);

This will show the varible on the page near the position wher it was inserted.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.