Page 1 of 1

how do you delete a category from a forum?

Posted: Fri Feb 24, 2012 10:01 am
by beginner123
I'm trying to create a forum with a mysql database and so far everything is working. The next thing I'm trying to do is delete a category. I have two types of users - admin and normal. Only admin users can create and delete categories.
Not sure if the code will help but heres all the code for the category so far

Heres the code for creating the category:

Code: Select all

<?php
include 'connect.php';
include 'header.php';

echo '<h2>Create a category</h2>';
if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1 )
{
	//the user is not an admin
	echo 'Sorry, you do not have sufficient rights to access this page.';
}
else
{
	//the user has admin rights
	if($_SERVER['REQUEST_METHOD'] != 'POST')
	{
		//the form hasn't been posted yet, display it
		echo '<form method="post" action="">
			Category name: <input type="text" name="cat_name" /><br />
			Category description:<br /> <textarea name="cat_description" /></textarea><br /><br />
			<input type="submit" value="Add category" />
		 </form>';
	}
	else
	{
		//the form has been posted, so save it
		$sql = "INSERT INTO categories(cat_name, cat_description)
		   VALUES('" . mysql_real_escape_string($_POST['cat_name']) . "',
				 '" . mysql_real_escape_string($_POST['cat_description']) . "')";
		$result = mysql_query($sql);
		if(!$result)
		{
			//something went wrong, display the error
			echo 'Error' . mysql_error();
		}
		else
		{
			echo 'New category succesfully added.';
		}
	}
}
include 'footer.php';
?>
and heres the code for displaying the category:

Code: Select all

<?php
include 'connect.php';
include 'header.php';

$sql = "SELECT
		cat_id,
		cat_name,
		cat_description
	FROM
		categories
	WHERE
		cat_id = " . mysql_real_escape_string($_GET['id']);

$result = mysql_query($sql);

if(!$result)
{
	echo 'The category could not be displayed, please try again later.' . mysql_error();
}
else
{
	if(mysql_num_rows($result) == 0)
	{
		echo 'This category does not exist.';
	}
	else
	{
		while($row = mysql_fetch_assoc($result))
		{
			echo '<h2>Topics in &prime;' . $row['cat_name'] . '&prime; category</h2><br />';
		}
	
		$sql = "SELECT	
			topic_id,
			topic_subject,
			topic_date,
			topic_cat
		FROM
			topics
		WHERE
			topic_cat = " . mysql_real_escape_string($_GET['id']);
		
		$result = mysql_query($sql);
		
		if(!$result)
		{
			echo 'The topics could not be displayed, please try again later.';
		}
		else
		{
			if(mysql_num_rows($result) == 0)
			{
				echo 'There are no topics in this category yet.';
			}
			else
			{
				//prepare the table
				echo '<table border="1">
					  <tr>
						<th>Topic</th>
						<th>Created at</th>
					  </tr>';	
					
				while($row = mysql_fetch_assoc($result))
				{				
					echo '<tr>';
						echo '<td class="leftpart">';
						echo '<h3><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><br /><h3>';
						echo '</td>';
						echo '<td class="rightpart">';
						echo date('d-m-Y', strtotime($row['topic_date']));
						echo '</td>';
					echo '</tr>';
				}
			}
		}
	}
}
include 'footer.php';
?>
I would really appreciate the help :)

Re: how do you delete a category from a forum?

Posted: Fri Feb 24, 2012 5:29 pm
by social_experiment
The removal of the category is easy

Code: Select all

<?php $sql = "DELETE FROM `table` WHERE `category` = '$category'"; ?>
The more difficult part is what to do with any articles already assigned to a category that is going to be removed. Will they be moved to a different category or removed along with the category? If the option the first one you will have to update each record related to the category. If not you simply have to remove the articles tied in with the category

Re: how do you delete a category from a forum?

Posted: Sat Feb 25, 2012 1:57 pm
by beginner123
If the category gets deleted then all the topics that in that category will get deleted to. Can you show me the code to delete the topics from the category?
I really appreciate the help

Re: how do you delete a category from a forum?

Posted: Sun Feb 26, 2012 3:55 pm
by social_experiment
Without specifics a detailed script is out of the question (and i think this section is about helping) but this pseudo code should be useful enough to get you started in the right direction

Code: Select all

 # assume fruit is the category to remove
 DELETE FROM categoryTable WHERE category = 'fruit'
 # now all articles (topics) have to be removed
 DELETE FROM articleTable WHERE category = 'fruit'
From the sample code you pasted it seems you know how to use php to communicate with a mysql server so this shouldn't be too difficult; if you created some code and you are still having problems with it you can paste it for more help

hth

Re: how do you delete a category from a forum?

Posted: Tue Feb 28, 2012 2:37 pm
by beginner123
@ social_experiment my forum will have loads of categories so I don't want to change the code everytime I want to delete one
I need it to delete the category I select. heres the code I have so far:

Code: Select all


$sql = "SELECT
	categoryID,
	categoryName,
	categoryDescription
                FROM
	     categories";
		
		$result = mysql_query($sql);
		echo '<form method="post" action="">
				Category:'; 
			                echo '<select name="topicCategory">';
				while($row = mysql_fetch_assoc($result))
				{
					echo '<option value="' . $row['categoryID'] . '">' . $row['categoryName'] . '</option>';
				}
				echo '</select><br />';	
				
				$sql = "DELETE categoryID,categoryName, categoryDescription 
					FROM 
						category";
				
				echo '<form method="post" action="">				
				<input type="submit" value="Delete category" />
				 </form>';
the first part of the code is just for selecting the category and so far nothing happens when I click the button

Re: how do you delete a category from a forum?

Posted: Tue Feb 28, 2012 3:12 pm
by social_experiment
beginner123 wrote:so I don't want to change the code everytime I want to delete one
You wont; 'fruit' is an example (pseudo-code isn't actual code).

Nothing will happen in your code because nothing happens when the button is clicked; after the button is clicked you should run the delete query:

Code: Select all

<?php
 // check if button is clicked
 if (isset($_POST['submitButton'])) {
    // get value from the dropdown list
    $category = $_POST['category'];
   
    $sql = mysql_query("DELETE FROM `categoryTable` WHERE `category` = '" . $category . "' ");
  }
?>

Re: how do you delete a category from a forum?

Posted: Tue Feb 28, 2012 3:49 pm
by beginner123
I added the code you gave but still nothing happens when i click the button. heres the code:

Code: Select all

$sql = "SELECT
					categoryID,
					categoryName,
					categoryDescription
				FROM
					categories";
		
		$result = mysql_query($sql);
		
		echo '<form method="post" action="">
				Category:'; 
		
				
				echo '<select name="topicCategory">';
					while($row = mysql_fetch_assoc($result))
					{
						echo '<option value="' . $row['categoryID'] . '">' . $row['categoryName'] . '</option>';
					}
				echo '</select><br />';	
				
				
				$sql = "DELETE categoryID,categoryName, categoryDescription 
					FROM 
						categories";
				
				echo '<form method="post" action="">				
				<input type="submit" value="Delete category" />
				 </form>';
					 
				// check if button is clicked
				if (isset($_POST['submitButton']))
				{
				// get value from the dropdown list
				$category = $_POST['category'];
    
				$sql = mysql_query("DELETE FROM `categories` WHERE `category` = '" . $category . "' ");  
				}

Re: how do you delete a category from a forum?

Posted: Tue Feb 28, 2012 3:57 pm
by social_experiment

Code: Select all

echo '<select name="topicCategory">';
The category value from the form you are using will be in $_POST['topicCategory']

Code: Select all

<?php
 echo '<form method="post" action="">                            
                                <input type="submit" value="Delete category" name="submitButton" />
                                 </form>';
?>
The submit button has no name; the code that checks if the button has been click is thus rendered void; up a name attribute to the submit button (as per the above example)

:) The code i give is merely an indication of what to do; use the variables / form values from your script for better results

Re: how do you delete a category from a forum?

Posted: Wed Feb 29, 2012 10:23 am
by beginner123
still nothing happens when I click the delete button
heres all the code again:

Code: Select all

$sql = "SELECT
					categoryID,
					categoryName,
					categoryDescription
				FROM
					categories";
		
		$result = mysql_query($sql);
		
		echo '<form method="post" action="">
				Category:'; 
		
				
				echo '<select name="topicCategory">';
					while($row = mysql_fetch_assoc($result))
					{
						echo '<option value="' . $row['categoryID'] . '">' . $row['categoryName'] . '</option>';
					}
				echo '</select><br />';	
				
				
				$sql = "DELETE categoryID,categoryName, categoryDescription 
					FROM 
						categories";
				
				echo '<form method="post" action="">				
				<input type="submit" value="Delete category" name="submitButton" />
				 </form>';
					 
				// check if button is clicked
				if (isset($_POST['submitButton']))
				{
				// get value from the dropdown list
				$category = $_POST['topicCategory'];
    
				$sql = mysql_query("DELETE FROM `categories` WHERE `category` = '" . $category . "' ");  
				}

Re: how do you delete a category from a forum?

Posted: Wed Feb 29, 2012 12:54 pm
by temidayo
beginner123 wrote:still nothing happens when I click the delete button
Separate the code that is displaying the categories from the
code that is supposed to delete

That way there will be less confusion.

Re: how do you delete a category from a forum?

Posted: Wed Feb 29, 2012 1:21 pm
by beginner123
ok this part of the code is for deleting:

Code: Select all

$sql = "DELETE categoryID,categoryName, categoryDescription 
                                        FROM 
                                                categories";
                                 
                                echo '<form method="post" action="">                            
                                <input type="submit" value="Delete category" name="submitButton" />
                                  </form>';
                                          
                                 // check if button is clicked
                                 if (isset($_POST['submitButton']))
                                 {
                                 // get value from the dropdown list
                                 $category = $_POST['topicCategory'];
     
                                $sql = mysql_query("DELETE FROM `categories` WHERE `category` = '" . $category . "' ");  
                                 }

Re: how do you delete a category from a forum?

Posted: Wed Feb 29, 2012 1:38 pm
by temidayo
The deleting part is not right. To delete you have to know what you want to delete.
Your delete code should look like this

Code: Select all

//ensure that a delete button is clicked before starting the delete process
 if (isset($_POST['submitButton'])){
  //get the category selected for deletion
   $category_to_delete = $_POST['topicCategory'];
 //create the delete SQL
  $sql = "DELETE  FROM   categories WHERE categoryID = '$category_to_delete' ";
 //run the sql query 
 mysql_query($sql);  
}

Re: how do you delete a category from a forum?

Posted: Wed Feb 29, 2012 1:46 pm
by temidayo
The complete code should be like this:

Code: Select all

/*Deleting part*/
//ensure that a delete button is clicked before starting the delete process
 if (isset($_POST['submitButton'])){
  //get the category selected for deletion
   $category_to_delete = $_POST['topicCategory'];
 //create the delete SQL
  $sql = "DELETE  FROM   categories WHERE categoryID = '$category_to_delete' ";
 //run the sql query 
 mysql_query($sql);  
}

/*Displaying part*/
$sql = "SELECT                   categoryID,
                                        categoryName,
                                        categoryDescription
                                FROM
                                        categories";
                
                $result = mysql_query($sql);
                
                echo '<form method="post" action="">
                                Category:'; 
                
                                
                                echo '<select name="topicCategory">';
                                        while($row = mysql_fetch_assoc($result))
                                        {
                                                echo '<option value="' . $row['categoryID'] . '">' . $row['categoryName'] . '</option>';
                                        }
                                echo '</select><br />'; 
                      
                                                       
                                echo ' <input type="submit" value="Delete category" name="submitButton" />'

                               echo'</form>';

Re: how do you delete a category from a forum?

Posted: Wed Feb 29, 2012 4:36 pm
by beginner123
thank you so much for the help it works :D

Re: how do you delete a category from a forum?

Posted: Wed Feb 29, 2012 4:47 pm
by temidayo
It's a pleasure to be of help.