Page 1 of 1

need help with code

Posted: Mon Mar 05, 2012 2:54 pm
by beginner123
I'm making a forum and I want to be able to delete a topic from a category but the code I wrote is getting errors and I'm not sure how to fix them

Code: Select all

//Deleting part
  if (isset($_POST['submitButton']))
  {
		$topic_to_delete = $_POST['topicCategory'];
		$sql = "DELETE  FROM   topics WHERE topicID = '$topic_to_delete' ";
		mysql_query($sql);  
   }
 
//Displaying part
 $sql = "SELECT    
		topicID
		topicSubject,
		topicDate,
		topicCategory,
		topicByUser
        FROM
            topics";
                 
                $result = mysql_query($sql);
                 
                echo '<form method="post" action="">
                                 Topic:'; 
                
                                
                 echo '<select name="topicCategory">';
                 while($row = mysql_fetch_assoc($result))
                  {
                       echo '<option value="' . $row['topicID'] . '">' . $row['topicCategory'] . '</option>';
                  }
                  echo '</select><br />'; 
                      
                  echo ' <input type="submit" value="Delete topic" name="submitButton" />
				  
 
                  </form>';
I'm getting an error with this line:

Code: Select all

echo '<option value="' . $row['topicID'] . '">' . $row['topicCategory'] . '</option>';
and if i click the delete button i get an error with this line:

Code: Select all

$topic_to_delete = $_POST['topicCategory'];
so I'm having trouble display the list of topics and since I can select a topic thats probably why I'm getting an error with the delete button

Hope someone can help :)

Re: need help with delete topic code

Posted: Mon Mar 05, 2012 2:56 pm
by Celauran
Your post is rather vague. Could you maybe provide some details on the error(s) you're getting?

Re: need help with delete topic code

Posted: Mon Mar 05, 2012 3:00 pm
by beginner123
the error is Undefined index: topicID

Re: need help with delete topic code

Posted: Mon Mar 05, 2012 3:10 pm
by Celauran
That isn't an error. That also isn't the entire message.

Re: need help with delete topic code

Posted: Mon Mar 05, 2012 3:20 pm
by beginner123
sorry the whole message is Notice: Undefined index: topicID in C:\wamp\www\forum\delete_topic.php on line 40
I didn't think the last part was important
any idea why I get this message?

Re: need help with delete topic code

Posted: Mon Mar 05, 2012 3:35 pm
by Celauran
You're trying to use a variable that may not exist. Without seeing the file in question, I can't say much more than that.

Re: need help with delete topic code

Posted: Wed Mar 07, 2012 1:15 pm
by beginner123
ok so I decided to change what I want this page to do. I want to have two drop down menus - one for the categories and the other for topics. When you chose a category from the first drop down menu I want the topics menu to populate with the topics from the category selected. When this is done then i'll focus on deleting the topic selected.
Heres the code I have so far. The category menu works fine but I don't know how to link the category menu to the topics menu and as I mentioned in my first post I am getting an error saying that Undefined index: topicID

Code: Select all

/Display the categories
 $sql = "SELECT            
		categoryID,
		categoryName,
		categoryDescription
        FROM
            categories";
                 
                $result = mysql_query($sql);
                 
                echo 'Category:';    
                 echo '<select name="Categories">';
                 while($row = mysql_fetch_assoc($result))
                  {
                       echo '<option value="' . $row['categoryID'] . '">' . $row['categoryName'] . '</option>';
                  }
                  echo '</select><br />'; 
                 
                  '</form>';

//Displaying the topics
 $sql = "SELECT    
		topicID
		topicSubject,
		topicDate,
		topicCategory,
		topicByUser
        FROM
            topics";
                 
                $result = mysql_query($sql);
                 
                echo '<form method="post" action="">
                                 Topics:'; 
                
                                
                 echo '<select name="topicSubject">';
                 while($row = mysql_fetch_assoc($result))
                  {
                       echo '<option value="' . $row['topicID'] . '">' . $row['topicSubject'] . '</option>';
                  }
                  echo '</select><br />'; 
               
                  echo ' <input type="submit" value="Delete topic" name="submitButton" />
                  </form>';
}
Hope someone can help :)

Re: need help with code

Posted: Wed Mar 07, 2012 4:27 pm
by social_experiment
Looks like a job for AJAX; Have a look at this url
http://examples.codecharge.com/Ajax/Dep ... stBox2.php

Re: need help with code

Posted: Thu Mar 08, 2012 4:11 am
by beginner123
so theres no other way to link two menus other than AJAX?

Re: need help with code

Posted: Thu Mar 08, 2012 6:51 am
by Celauran
You could display the parent form, have the user click submit, then display the appropriate child form, but that's messy. AJAX is the better option.

Re: need help with code

Posted: Thu Mar 08, 2012 7:16 am
by beginner123
ok thanks i'll try it
but could you help with the problem in the first post of this thread? about the message I am getting with topicID

Re: need help with code

Posted: Thu Mar 08, 2012 7:36 am
by Celauran
If you answer my questions and post the complete code, sure.

Re: need help with code

Posted: Thu Mar 08, 2012 1:24 pm
by beginner123
its solved. thanks for the help

Re: need help with delete topic code

Posted: Thu Mar 08, 2012 8:32 pm
by califdon
beginner123 wrote:sorry the whole message is Notice: Undefined index: topicID in C:\wamp\www\forum\delete_topic.php on line 40
I didn't think the last part was important
any idea why I get this message?
Every bit of an error message is important!