Welcome to Part 3 of this 5 part tutorial on making a complete Tutorial Management System. This is probably the most important section of the 5 because it is dealing with managing the tutorials themselves. Every page we create in this section will go into the admin folder.
We will start the same way we did in the last section, with the add records page. Create a page called add.php and put this code into it.
$result = mysql_query($query) or die("Error in query: $query . " . mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_row($result))
{
echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}
}
else
{
echo '<option>No Catagories</option>';
}
?>
</select><br><br>
<h2>Skill Level</h2>
<input name="skill" type="text" size="50"><br><br>
<h2>Brief Description</h2>
<textarea name="brief" cols="50" rows="4"></textarea><br><br>
<h2>Tutorial</h2>
<textarea name="tutorial" cols="80" rows="30"></textarea><br><br>
<h2>Author</h2>
<input name="person" type="text" size="50"><br>
<p>
<input name="submit" type="Submit" value="Submit">
<input type="reset">
</p>
</form>
<?php
}
else
{
$name = $_POST['name'];
$keywords = $_POST['keywords'];
$avatar = $_POST['avatar'];
$category = $_POST['category'];
$skill = $_POST['skill'];
$brief = $_POST['brief'];
$tutorial = $_POST['tutorial'];
$person = $_POST['person'];
$query = "INSERT INTO tutorials (name, keywords, avatar, category, skill, brief, tutorial, person, timestamp) VALUES ( '".$name."', '".$keywords."', '".$avatar."', '".$category."', '".$skill."', '".$brief."', '".$tutorial."', '".$person."', NOW())";
mysql_query($query)or die(mysql_error());
echo 'Tutorial Successfully added. <a href="index.php">Go back to admin home page</a>';
}
mysql_close($connection);
?>
Ok, most of this document is pretty straight forward; it’s the category part that needs explanation. It will fetch the categories from the category table and output them into the drop down menu. When you select an item and write the record to ‘tutorials’, in the category section it will contain the categories ID number, not its name. That is very important to remember. So for example if ‘photoshop’ was the third option on your list and its ID was 3, if you were to create a record under the category ‘photoshop’ can echo its category information, it would echo ‘3’ rather then ‘photoshop’.
Lets move on shall we. Now we create the edit.php page. Add the following code to it.
if (!isset($_POST['submit']))
{
if ((!isset($_GET['id']) || trim($_GET['id']) == ''))
{
die ('Missing record ID!');
}
$id = $_GET['id'];
$query = "SELECT * FROM tutorials WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
if (mysql_num_rows($result) > 0)
{
$row = mysql_fetch_row($result);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<h1>Edit Tutorial #<?php echo $row[0]; ?></h1>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<h2>Title</h2>
<input value="<?php echo stripslashes($row[1]); ?>" name="name" type="text" size="50"><br>
<br>
<h2>Key Words</h2>
<textarea name="keywords" cols="50" rows="4"><?php echo stripslashes($row[2]); ?></textarea><br><br>
<h2>Avatar</h2>
<input name="avatar" value="<?php echo stripslashes($row[2]); ?>" type="text" size="50"><br><br>
<h2>Category</h2>
<input value="<?php echo stripslashes($row[4]); ?>" name="category" type="text" size="10"><br><br>
<h2>Skill Level</h2>
<input name="skill" type="text" value="<?php echo stripslashes($row[5]); ?>" size="50"><br><br>
<h2>Brief Description</h2>
<textarea name="brief" cols="50" rows="4"><?php echo stripslashes($row[6]); ?></textarea><br><br>
<h2>Tutorial</h2>
<textarea name="tutorial" cols="80" rows="30"><?php echo stripslashes($row[7]); ?></textarea>
<br>
<br>
<h2>Writer</h2>
<input value="<?php echo stripslashes($row[8]); ?>" name="person" type="text" size="50"><br>
<p>
<input name="submit" type="Submit" value="Submit">
<input type="reset">
</p>
</form>
<?php
}
else
{
echo 'Tutorial not found!';
}
}
else
{
$id = $_POST['id'];
$name = $_POST['name'];
$keywords = $_POST['keywords'];
$avatar = $_POST['avatar'];
$category = $_POST['category'];
$skill = $_POST['skill'];
$brief = $_POST['brief'];
$tutorial = $_POST['tutorial'];
$person = $_POST['person'];
$query = "UPDATE tutorials SET name = '".$name."', keywords = '".$keywords."', avatar = '".$avatar."', category = '".$category."', skill = '".$skill."', brief = '".$brief."', tutorial = '".$tutorial."', person = '".$person."' WHERE id = '".$id."'";
$result = mysql_query($query)or die(mysql_error());
echo 'Tutorial successfully edited. <a href="index.php">Go back to admin home page</a>';
}
mysql_close($connection);
?>
This edit page works the same way that edit_category does. One thing to remember is that when picking the tutorials category in this edit page is that it wont give you a drop down menu, but instead display the categories ID number. To change it you just need to change the number accordingly. If you are unsure of a categories ID number look at category.php and it will display the ID of every category in front of its name.
Now for the last page of this section. That’s right, the last, short section this one. Create a page called delete.php and add the following code.
if ((!isset($_GET['id']) || trim($_GET['id']) == ''))
{
die('Missing record ID!');
}
$id = $_GET['id'];
$query = "DELETE FROM tutorials WHERE id = '".$id."'";
$result = mysql_query($query)
or die ("Error in query : $query. " . mysql_error());
$query = "DELETE FROM comments WHERE id = '$id'";
$result = mysql_query($query)
or die ("Error in query : $query. " . mysql_error());
echo 'Tutorial records successfully deleted';
mysql_close($connection);
?>
This is much like the other delete page (delete_category.php) except a little less complicated. This one fetched the id from the URL, deletes all comments on that tutorial and then that tutorial.
Thank you for reading the third instalment of this tutorial. The links below will take you to the other sections of the tutorial.
« Previous [1] [2] [3] [4] [5] Next » | Tutorial System ZIP (124)
No comments yet.
RSS feed for comments on this post. TrackBack URL