Hello everybody,

I'm using the following script inorder to delete subtopics.But, before they actually get deleted I'm checking if there are any articles under them.
Well, I have topics and under each topic i can have subtopics and under each subtopic i can have articles.

When I try to delete the subtopics though there are actually articles present under them, they get deleted and when there are no articles, it gets deleted too.

I tried to debug, it always returns that there are no articles under the subtopic which I've marked for deletion.But, there are articles under that sub-topic.

I de-bugged in a different way, like giving the sub-topic id no directly and it worked, that is it has shown me the number if there are articles present or not.

This is the query which I used to check if there are any articles present or not

PHP Code:
$result_sub=mysql_query("SELECT * FROM articles WHERE SUBTOPIC_ID = 2");
   
$number mysql_num_rows($result_sub); 
   echo(
"$number"); 

This is my actual script.I first list all topics and select a topic and it shows me all sub-topics with a check box.I check the checkbox and click the button delete.It has to be deleted only if there are no articles and if it has, I need to show some message.

Could somebody please help me out in-order to fix my script.

Thanks in advance

PHP Code:
<?
//Db connection

include "dbconnection.oam";
?>

<?php 
// if you have chosen subtopics to delete 
if(isset($HTTP_POST_VARS['action'])){ 
   
$qry3="SELECT SUBTOPIC_ID FROM subtopic WHERE SUBTOPIC_ID='".$HTTP_POST_VARS['SUBTOPIC_ID']."'"
   
$result_3=mysql_query($qry3) or die(mysql_error()); 
// begin the query  
   
$subtopic_id=$HTTP_POST_VARS['SUBTOPIC_ID'];
   echo 
$subtopic_id
   
$result_sub=mysql_query("SELECT * FROM articles WHERE SUBTOPIC_ID='$subtopic_id' ");
   
$number mysql_num_rows($result_sub); 
   echo(
"$number"); 


   
$qry4 "DELETE FROM subtopic WHERE SUBTOPIC_ID IN('" implode("','"$HTTP_POST_VARS['to_delete']) . "')";

   while(
$row_4=mysql_fetch_row($result_sub)){ 
      if(isset(
$HTTP_POST_VARS[$row_4->SUBTOPIC_ID])){  
// the checkboxes were named for the subtopic_id 
         
$qry4 .= "OR SUBTOPIC_ID=".$row_4->SUBTOPIC_ID
// add this subtopic to the query string 
      

   } 
   
mysql_query($qry4) or die(mysql_error()); 
}


// get the list of topic names 
$qry1="SELECT TOPIC_ID, TOPIC_NAME FROM topic ORDER BY TOPIC_NAME"
$result_1=mysql_query($qry1) or die(mysql_error()); 

// if you have chosen a topic, get the list of subtopics 
if(isset($HTTP_POST_VARS['TOPIC_ID'])){ 
   
$qry3="SELECT SUBTOPIC_ID, SUBTOPIC_NAME, TOPIC_ID FROM subtopic WHERE TOPIC_ID='".$HTTP_POST_VARS['TOPIC_ID']."' ORDER BY SUBTOPIC_NAME"
   
$result_3=mysql_query($qry3) or die(mysql_error()); 


?> 

<HTML> 
<!--- create the topic select box ---> 
<form name="topic_form" action="<?=$PHP_SELF?>" method="post"> 
   <select name="TOPIC_ID"> 
      <option value="">Choose a topic</option> 
<?

    
while($row=mysql_fetch_object($result_1)){ 
    echo 
"<option value=\"".$row->TOPIC_ID."\">".$row->TOPIC_NAME."</option>"
      }
?> 
   </select> 
   <input type="submit" value="Set Topic"> 
</form> 
 
   <!--- include topic_ID for continuity ---> 
   <input type="hidden" name="TOPIC_ID" value="<?=$HTTP_POST_VARS['TOPIC_ID']?>"> 
   
</form> 

<!--- create the list of subtopics ---> 
<form name="delete_form" action="<?=$PHP_SELF?>" method="post"> 
<?
   
if(isset($HTTP_POST_VARS['TOPIC_ID'])){ 
   while(
$row_3=mysql_fetch_object($result_3)){ 
// subtopics named for the subtopic_id (aids in ease of retrieval after submittal 
  
echo "<input type=\"checkbox\" name=\"to_delete[]\" value=\"".$row_3->SUBTOPIC_ID."\">".$row_3->SUBTOPIC_NAME."<BR>";


      } 
   }
?> 

   <input type="hidden" name="TOPIC_ID" value="<?=$HTTP_POST_VARS['TOPIC_ID']?>"> 
  
<!--- "action" defined so there is a definite switch to determine if things should be deleted ---> 
   <input type="hidden" name="action" value="delete"> 
   <input type="submit" value="Delete"> 
<FORM>
<INPUT TYPE="Button" VALUE="Back" 
onClick="window.location= 'menu.php' "> 
</FORM>
</form> 
</HTML>