need help with code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

need help with code

Post 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 :)
Last edited by beginner123 on Wed Mar 07, 2012 1:16 pm, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help with delete topic code

Post by Celauran »

Your post is rather vague. Could you maybe provide some details on the error(s) you're getting?
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: need help with delete topic code

Post by beginner123 »

the error is Undefined index: topicID
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help with delete topic code

Post by Celauran »

That isn't an error. That also isn't the entire message.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: need help with delete topic code

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help with delete topic code

Post 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.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: need help with delete topic code

Post 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 :)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: need help with code

Post by social_experiment »

Looks like a job for AJAX; Have a look at this url
http://examples.codecharge.com/Ajax/Dep ... stBox2.php
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: need help with code

Post by beginner123 »

so theres no other way to link two menus other than AJAX?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help with code

Post 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.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: need help with code

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: need help with code

Post by Celauran »

If you answer my questions and post the complete code, sure.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: need help with code

Post by beginner123 »

its solved. thanks for the help
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: need help with delete topic code

Post 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!
Post Reply