To delete records after a condition is satisfied

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • To delete records after a condition is satisfied

    Hello everybody,

    I have topics and each topic has many subtopics and each subtopic has articles.
    Ok now, I'm listing all the topic names and select a topic name to show all the subtopics under them.Now, I select a subtopic name and press the button delete.If there are no articles under this subtopic then the subtopic is deleted else it has to show me the message that there are articles under them.I found a simple error which I'm not able to figure out that is, when I select a subtopic name which has articles under it and press the button delete, I get the mesg: There are articles existing under this sub-topic. and also it shows me the "QUERY IS EMPTY"

    How can I avoid this one??


    Thanks in advance


    PHP-Code:
    <?
    include "../dbconnection.dam"
    ?>
    <?php 
    // if you have chosen subtopics to delete 
        
    if(isset($HTTP_POST_VARS['action'])){ 
        
    $subtopic_id $HTTP_POST_VARS['SUBTOPIC_ID']; 
        
    $qry3 "SELECT TOPIC_ID FROM subtopic WHERE SUBTOPIC_ID='$subtopic_id'"
        
    $result_3 mysql_query($qry3) or die(mysql_error()."<br />SQL: $qry3); 

    // begin the query 
     
    foreach( $HTTP_POST_VARS"to_delete" ] as $key => $value )
    {
       
    $sql "SELECT SUBTOPIC_ID FROM articles WHERE SUBTOPIC_ID = '" $value "'";
       
    $result_sub mysql_query($sql) or die(mysql_error()."<br />SQL: $sql);
       if ( 
    mysql_num_rows$result_sub ) > )
        {
           echo 
    'There are articles existing under this sub-topic';
        }else
           
    // there are no articles - safe to delete 
       
    $qry4 "DELETE FROM subtopic WHERE subtopic_id IN('" implode("','"$HTTP_POST_VARS['to_delete']) . "')"
       while( 
    $row_4 mysql_fetch_row($result_3) ){ 
       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>
    Zuletzt geändert von minds_gifts; 27.04.2003, 14:17.
    Happiness is a state of mind.
    Change your mind and be happy!

  • #2
    it looks like you want sth. like an altert window. if yes you will have to use java script.

    Kommentar


    • #3
      whats sth??
      yeah, some sort of alert window.I can write it in java-script.I figured out to use radio buttons rather than using checkboxes in-order to restrict the user to delete only one subtopic at a time.I've no idea if this gonna be a better solution.

      Thanks
      Happiness is a state of mind.
      Change your mind and be happy!

      Kommentar


      • #4
        sth. = something
        i'm not a java-script expert, so you will have to ask somebody different

        Kommentar


        • #5
          Well I dont know whether this answers your question, but this is how to show an alert box in Javascript:

          <SCRIPT language=javascript>
          alert("Message");
          </SCRIPT>

          Do you have a live example of your problem? Reading code on sundays is very tiresome
          Zuletzt geändert von pekka; 27.04.2003, 14:25.

          Kommentar


          • #6
            Well, I'm aware of the java-script alert window.
            My question was why does I get the message "QUERY IS EMPTY" when i try to delete the subtopics which have articles under it.How can I avoid this message.

            Thank you
            Happiness is a state of mind.
            Change your mind and be happy!

            Kommentar


            • #7
              Original geschrieben von minds_gifts
              My question was why does I get the message "QUERY IS EMPTY" when i try to delete the subtopics which have articles under it.How can I avoid this message.
              uhm, you've got a lot of queries in your script, so it's hard to figure out which one is mentioned by this message "query is empty".

              i assume that this message is created by one of your
              PHP-Code:
              or die(mysql_error); 
              statements.

              could you perhaps extend each of these statements, so that it also shows which query ist actualy effected?

              i'm thinking of something like this,
              PHP-Code:
              or die("Query #1 error: ".mysql_error); 
              I don't believe in rebirth. Actually, I never did in my whole lives.

              Kommentar


              • #8
                Obviously this is the one which is killing me.
                PHP-Code:
                or die(mysql_error); 
                and thats related to qry4.

                Any idea whats going wrong with qry4???

                Thank you
                Happiness is a state of mind.
                Change your mind and be happy!

                Kommentar


                • #9
                  Aaah! A DELETE statement returns null, of course, because there are no results of any kind. Try running the query without the "or die" threat, that should work.

                  Kommentar


                  • #10
                    and if you wanna be sure that there really was something deleted, check if mysql_affected_rows() returns a value greater than 0.
                    I don't believe in rebirth. Actually, I never did in my whole lives.

                    Kommentar

                    Lädt...
                    X