delete two values...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azura
    New Member
    • Jul 2008
    • 47

    delete two values...

    hello sir... i had a problem... how can i delete two values?? before this i delete in one value only and it's work.. but how can i delete if i had two value...?? this is my coding for one value...

    [PHP]if(isset($_POST['del_id']) && sizeof($_POST['del_id'])>0){
    for($i=0;$i<cou nt($_POST['del_id']);$i++) {
    $del = mysql_query("DE LETE FROM `stud_sub` WHERE `matric_no`='". $_POST['del_id'][$i]."'");//
    }
    }[/PHP]

    [HTML] <input name="del_id[]" type="checkbox" value="<?php echo $row_reg['matric_no']; ?>" />[/HTML]
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    I don't see anything in your logic. You might have an error in your HTML. Do a view source and see if all the check boxes don't have the same ID.

    On another note, I've optimized your PHP loop for efficiency and performance and included SQL Injection preventions.

    [PHP]
    $idArr = isset($_POST['del_id']) ? $_POST['del_id'] : "";

    for( $i=0; $i < isset($idArr[$i]); $i++ )
    {
    $cleanID = mysql_real_esca pe_string($idAr r[$i]); // this prevents SQL Injection
    if(!mysql_query ("DELETE FROM `stud_sub` WHERE `matric_no`='" . $cleanID . "'"))
    {
    die("Error Occured In SQL: " . mysql_error());
    }
    else
    {
    die("ID's Deleted!");
    }
    }
    [/PHP]

    Let me know if any questions,





    Dan

    Comment

    • zabsmarty
      New Member
      • Feb 2007
      • 25

      #3
      or you will use for each loop when getting value in array

      Code:
      foreach ($_POST['del_id'] as $chk)
      {
      $sqldelall = "delete from std_sub  where matric_no =".$chk;
      mysql_query ($sqldelall, $db_link) or die (mysql_error() .FETCH_DATA );
      }
      Last edited by Markus; Oct 28 '08, 10:52 AM. Reason: added # tags

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by zabsmarty
        or you will use for each loop when getting value in array

        Code:
        foreach ($_POST['del_id'] as $chk)
        {
        $sqldelall = "delete from std_sub  where matric_no =".$chk;
        mysql_query ($sqldelall, $db_link) or die (mysql_error() .FETCH_DATA );
        }
        Use code tags when posting, Zab.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by zabsmarty
          or you will use for each loop when getting value in array

          Code:
          foreach ($_POST['del_id'] as $chk)
          {
          $sqldelall = "delete from std_sub  where matric_no =".$chk;
          mysql_query ($sqldelall, $db_link) or die (mysql_error() .FETCH_DATA );
          }
          Now what if I were to post this as the "del_id"?
          Code:
          1 OR matric_no != 1;
          You would end up with an awfully empty table...

          My point being:
          NEVER use any user input in a SQL query without validating it first. Especially not when you are dealing with DELETE or UPDATE queries.

          If you are expecting a number, make sure you are getting a number.
          If you are expecting a string, run it through mysql_real_esca pe_string first.

          See this article if you need some more convincing.

          Comment

          Working...