Problems deleting mutiple records

Discussion in 'PHP' started by ghadacr, May 17, 2007.

  1. #1
    I'm trying to delete mutiple records upon a selection of check boxes, but the scipt does not delete records if i select more than one, but insteads deletes only one record.

    Here is the code:

    updateuser. php

    php:

    <?PHP include 'opendb.php'; ?>


    <?PHP include 'header.php'; ?>
    <?PHP
    //$checkbox;
    $sql="SELECT * FROM Users";
    $result=mssql_query($sql);

    $count=mssql_num_rows($result);

    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="Userdelete.php">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td colspan="3" bgcolor="#FFFFFF"><strong>Delete Users</strong> </td>
    </tr>
    <tr>
    <td align="center" bgcolor="#FFFFFF">#</td>
    <td align="center" bgcolor="#FFFFFF"><strong>UserID</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>UserName</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>UserInitials</strong></td>
    </tr>
    <?php
    while($rows=mssql_fetch_array($result)){
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['UserID']; ?>"></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['UserID']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['UserName']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['UserInitials']; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="delete"></td>
    </tr>
    <?php
    // Check if delete button active, start this
    $checkbox="checkbox[]";
    if($delete = 'delete'){
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sqld = "DELETE FROM Users WHERE UserID='$del_id'";
    //$result3 = mssql_query($sqld);
    }

    // if successful redirect to delete_multiple.php
    if($result3 = mssql_query($sqld) ){
    echo "<meta http-equiv= content=\"0;URL=Userdelete.php\">";
    }
    else echo"Problem deleting";
    }
    mssql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>



    Userdelete.php

    php:

    <?PHP include 'opendb.php'; ?>


    <?PHP include 'header.php'; ?>

    <?php

    $UserName = 'UserName'
    $UserInitials = $_GET['UserInitials'];
    $UserID = $_GET['UserID'];

    $query="UPDATE Users SET UserName='$UserName', UserInitials='$UserInitials' WHERE UserID='$UserID'";

    mssql_query($query);
    echo "Record Updated";
    mssql_close();
    ?>

    <?PHP include 'footer.php'; ?>
     
    ghadacr, May 17, 2007 IP
  2. gfreeman

    gfreeman Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try using == in your if statements
     
    gfreeman, May 17, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    mysql_query("DELETE FROM Users WHERE UserID IN(" . implode(', ', array_map('intval', (array)$_POST['checkbox'])) .")") OR die(mysql_error());
    
    PHP:
     
    nico_swd, May 17, 2007 IP
  4. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No still does not work
     
    ghadacr, May 17, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Do you get any errors?
     
    nico_swd, May 17, 2007 IP
  6. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    At the bottom put mysql_error() and see what it says (if anything).
     
    CodyRo, May 17, 2007 IP
  7. Estevan

    Estevan Peon

    Messages:
    120
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    0
    #7
    hello
    use this
    no need count all mysql num rows
    $count=mssql_num_rows($result);


    count only checkboxs submited

    $count=count($_POST['checkbox']);
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sqld = "DELETE FROM Users WHERE UserID='$del_id'";
    //$result3 = mssql_query($sqld);
    }
     
    Estevan, May 17, 2007 IP
  8. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Works, Thanks Everyone!!!!
     
    ghadacr, May 17, 2007 IP