Delete mysql records with array_diff

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pezholio
    New Member
    • Jun 2007
    • 22

    Delete mysql records with array_diff

    Hi,

    I'm trying to put together a page that deletes records from a database based on if an item is unticked, I've got the existing items in an array (let's call it array1 for the sake of argument):

    Code:
    Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
        [6] => 7
        [7] => 8
        [8] => 9
        [9] => 10
        [10] => 11
        [11] => 12
        [12] => 13
        [13] => 14
        [14] => 15
    )
    and the remaining items after the form has been submitted in and array called array2:

    Code:
    Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
        [6] => 7
        [7] => 8
        [8] => 9
        [9] => 10
        [10] => 11
    )
    I then try and compare the arrays using array_diff and delete the remaining arrays from the database by doing the following:


    [PHP]
    foreach (array_diff($ar ray1, $array2) as $delete) {
    $query = "DELETE FROM database WHERE wasteid = '$delete' AND id = '$id' ";
    mysql_query($qu ery)
    or die(mysql_error ());
    }
    [/PHP]

    But it doesn't seem to work! Any ideas?

    Cheers
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Read the manual.
    array_diff() returns an array containing all the values of array1 that are NOT present in array2.
    And are you sure you mean DELETE FROM `database`?

    Comment

    • pezholio
      New Member
      • Jun 2007
      • 22

      #3
      Originally posted by code green
      Read the manual.
      array_diff() returns an array containing all the values of array1 that are NOT present in array2.
      And are you sure you mean DELETE FROM `database`?
      Nah, I meant 'table', but you get the idea :)

      The usage of array_diff is right I think, if I print_r it I get:

      Code:
            Array
            (
                [10] => 11
                [11] => 12
                [12] => 13
                [13] => 14
                [14] => 15
            )
      which, in my script is the boxes that have been left unchecked.

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by pezholio
        Hi,

        I'm trying to put together a page that deletes records from a database based on if an item is unticked, I've got the existing items in an array (let's call it array1 for the sake of argument):


        I then try and compare the arrays using array_diff and delete the remaining arrays from the database by doing the following:


        [PHP]
        foreach (array_diff($ar ray1, $array2) as $delete) {
        $query = "DELETE FROM database WHERE wasteid = '$delete' AND id = '$id' ";
        mysql_query($qu ery)
        or die(mysql_error ());
        }
        [/PHP]

        But it doesn't seem to work! Any ideas?

        Cheers
        If you have set up your form (Basically Dynamic Form) Like in this format.

        This will print inside while loop which uses mysql_fetch_ass oc

        Code:
        <input type="checkbox" name="frm_chk_delete[]" value="1001" />
        <input type="checkbox" name="frm_chk_delete[]" value="1002" />
        <input type="checkbox" name="frm_chk_delete[]" value="1005" />
        On the Server Side you Delete the that Check box array Related Values.

        [code=php]
        <?php
        $frm_chk_delete = $_POST['frm_chk_delete '];
        $list_of_ids = implode(",", $frm_chk_delete );
        mysql_query("DE LETE FROM myTable WHERE myid IN ($list_of_ids)" );
        ?>
        [/code]

        So It will act like this

        [CODE=sql] DELETE FROM myTable WHERE myid IN ('1001','1002', '1005')[/CODE]

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          I have to change your Original Thread title:
          array_diff woes
          Which was not a good Thread Title. Please find out how to create a good thread title

          Comment

          • pezholio
            New Member
            • Jun 2007
            • 22

            #6
            Originally posted by ajaxrand
            I have to change your Original Thread title:
            array_diff woes
            Which was not a good Thread Title. Please find out how to create a good thread title
            Ah, thanks! :) I think it's the journalist in me trying to create clever titles! Your solution worked like a charm by the way. Any ideas why my original code didn't work?

            Cheers

            Comment

            • Nert
              New Member
              • Nov 2006
              • 64

              #7
              Originally posted by pezholio
              I then try and compare the arrays using array_diff and delete the remaining arrays from the database by doing the following:


              [PHP]
              foreach (array_diff($ar ray1, $array2) as $delete) {
              $query = "DELETE FROM database WHERE wasteid = '$delete' AND id = '$id' ";
              mysql_query($qu ery)
              or die(mysql_error ());
              }
              [/PHP]

              But it doesn't seem to work! Any ideas?

              Cheers
              In your WHERE statement, I find " wastedid='$dele te' AND id='$id' " . But where did you get the value for " $id " ?


              .: Nert :. (^_^)

              Comment

              • ak1dnar
                Recognized Expert Top Contributor
                • Jan 2007
                • 1584

                #8
                Originally posted by Nert
                In your WHERE statement, I find " wastedid='$dele te' AND id='$id' " . But where did you get the value for " $id " ?
                Indeed ! Thats the thing nert.
                Originally posted by pezholio
                Your solution worked like a charm by the way. Any ideas why my original code didn't work?
                I think this Example will clearly explains what have you done ealier.
                Just Excute this code snipet.

                [CODE=php]<?php
                $array1 = array("a" => "green","bl ue", "red","pink","w hite");
                $array2 = array("b" => "green", "yellow", "red");
                foreach (array_diff($ar ray1, $array2) as $delete) {
                $query = "DELETE FROM database WHERE wasteid = '$delete' AND id = '$id' ";
                echo $query.'<br>';
                }
                ?>[/CODE]

                Originally posted by pezholio
                Your solution worked like a charm by the way
                Glad to here that from you.Good luck! Hope to see you again.

                Comment

                • Nert
                  New Member
                  • Nov 2006
                  • 64

                  #9
                  Originally posted by ajaxrand
                  I think this Example will clearly explains what have you done ealier.
                  Just Excute this code snipet.

                  [CODE=php]<?php
                  $array1 = array("a" => "green","bl ue", "red","pink","w hite");
                  $array2 = array("b" => "green", "yellow", "red");
                  foreach (array_diff($ar ray1, $array2) as $delete) {
                  $query = "DELETE FROM database WHERE wasteid = '$delete' AND id = '$id' ";
                  echo $query.'<br>';
                  }
                  ?>[/CODE]
                  Yah, this script would make it clearer.

                  Anyway, have you made your code working Pez?

                  Comment

                  • pezholio
                    New Member
                    • Jun 2007
                    • 22

                    #10
                    Yeah, got it working great. The ID was specified elsewhere in the script, but I think Rand's solution is neater :)

                    Thanks for all your help guys!

                    Comment

                    • Nert
                      New Member
                      • Nov 2006
                      • 64

                      #11
                      Originally posted by pezholio
                      Yeah, got it working great. The ID was specified elsewhere in the script, but I think Rand's solution is neater :)

                      Thanks for all your help guys!

                      That's great, good luck. (^_^)



                      .: Nert :.

                      Comment

                      Working...