Multiple records delete without using array checkbox

Discussion in 'PHP' started by kumar_rajeev47, Mar 16, 2009.

  1. #1
    Hi everyone,

    i have a problem in php. My Problem is that as follows:

    i have a checkbox and name checkbox1,checkbox2,checkbox3 etc which id dynamically generated in php. ? Hoe can i get this values from php and how can i delete multiple records using this concept in php. Any one can help me.

    Thanks,
    Rajeev
     
    kumar_rajeev47, Mar 16, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    just give them an array name like
    input name=checkboxes[] type=checkbox
    and do a foreach() into the php side
     
    crivion, Mar 16, 2009 IP
  3. kumar_rajeev47

    kumar_rajeev47 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    This is the code
    <input type="checkbox" name="chk<?=$sn?>" id="chk<?=$sn?>" value="<?=$Detail->id?>"

    Here is:
    $sn is the serial number which is generated through code
    $Detail->id is the id of the particular checkbox.

    Please help me.

    Rajeev
     
    kumar_rajeev47, Mar 16, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If I correctly understand your explanation:

    $to_delete = array();
    foreach ($_REQUEST as $k => $v)
       if (preg_match('/^chk\d+$/', $k))
          $to_delete[] = intval($v);
    $to_delete_str = join(', ', array_unique($to_delete));
    $sql = "delete from my_table where id in ("{$to_delete_str}");
    Code (markup):
     
    SmallPotatoes, Mar 16, 2009 IP
  5. kumar_rajeev47

    kumar_rajeev47 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    this seems some work but when we delete multiple records it only delete the first record not last one. please help me.

    Rajeev
     
    kumar_rajeev47, Mar 16, 2009 IP
  6. CGSS-CyberGeek

    CGSS-CyberGeek Peon

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Okay, simple. (Let me know if this is what you are looking for)

    First off, you should be simply creating your HTML checkbox inputs as follows:

    <input type="checkbox" name="checkbox[]" value="1" />
    Code (markup):
    Now, for PHP code. If your submit button's name is submit something perhaps like this will work then:

    
    <?php
    if( TRUE == isset($_POST['submit']) ) {
    /* $check_box_ids now will contain an array of all check box values that have been checked. */
    $check_box_ids = $_POST['checkbox'];
    
    /* Make sure that check_box_ids count is greater then 0, else we will run into problems
    when executing mysql_query */
    if( 0 < count($check_box_ids) ) {
    /* Do your operation here .. Perhaps an SQL query */
    $sql = "DELETE FROM `item_table` WHERE primary_key IN (" . implode(',', $check_box_ids) . ")";
    $mysql_rid = mysql_query($sql);
    }
    }
    Code (markup):
    Now, does this help you? If not, let me know.
     
    CGSS-CyberGeek, Mar 16, 2009 IP
  7. kumar_rajeev47

    kumar_rajeev47 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi
    not this is not helpful for me.
     
    kumar_rajeev47, Mar 16, 2009 IP
  8. CGSS-CyberGeek

    CGSS-CyberGeek Peon

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Mhmm.
    Okay, well then.

    Do you perhaps have a bit more detail/information about your code:

    What exactly is the meaning for the serial number? Is it a unique value continuously? Are the checkbox items for items all within the same Database table?

    It would be helpful to have a bit more of an explanation on what you are trying to do with the data that is posted.
     
    CGSS-CyberGeek, Mar 16, 2009 IP
  9. kumar_rajeev47

    kumar_rajeev47 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is the code
    <input type="checkbox" name="chk<?=$sn?>" id="chk<?=$sn?>" value="<?=$Detail->id?>"

    Here is:
    $sn is the serial numbe eg:1,2,3,4 etc (means: chk1,ch2,chk3,chk4)
    $Detail->id is the id of the particular checkbox.
     
    kumar_rajeev47, Mar 16, 2009 IP
  10. kumar_rajeev47

    kumar_rajeev47 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    $to_delete = array();
    foreach ($_REQUEST as $k => $v)
    if (preg_match('/^chk\d+$/', $k))
    $to_delete[] = intval($v);
    $to_delete_str = join(', ', array_unique($to_delete));
    $sql = "delete from my_table where id in ("{$to_delete_str}");

    This is the code send to me some. it works but when we delete the first and last record from selecting the checkbox it only deletes the first record not last one( Eg: when we celete checkbox1,checkbox2 it only delete the checkbox1 not the checkbox2)

    Rajeev
     
    kumar_rajeev47, Mar 16, 2009 IP
  11. CGSS-CyberGeek

    CGSS-CyberGeek Peon

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Okay.

    May I ask why you are creating checkbox1, checkbox2, checkbox3 and you can not just use an array of them?

    Do all the checkbox values contain a primary key or an identity value within a database? If so, are they all in the same table?
     
    CGSS-CyberGeek, Mar 16, 2009 IP
  12. kumar_rajeev47

    kumar_rajeev47 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    hi grunt,


    $to_delete = array();
    foreach ($_REQUEST as $k => $v)
    if (preg_match('/^chk\d+$/', $k))
    $to_delete[] = intval($v);
    $to_delete_str = join(', ', array_unique($to_delete));
    $sql = "delete from my_table where id in ("{$to_delete_str}");

    this code works. but you it deletes one record not lastone. actually we can not change the whole code. if it possible to only modify this code and works fine.

    Thanks,
    Rajeev
     
    kumar_rajeev47, Mar 16, 2009 IP
  13. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #13
    If it's not deleting all the checked items then whoever wrote that code (me) didn't understand what your form looks like. Maybe you need to provide a sample of the form's HTML, and also to explain which field you are using as your primary key in the database, and where that field appears in the form.
     
    SmallPotatoes, Mar 16, 2009 IP
  14. Awon

    Awon Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hello There

    I come to know you about PHP Mysql-Deleting multiple records using checkbox.

    I have a url which can help you.

    Click Here : http://www.raiseitsolutions.com/forum/viewtopic.php?f=5&t=121

    I hope it is enough for you.

    Thank You

    AWON
     
    Awon, Jul 29, 2011 IP