Delete from checkboxes not working

Discussion in 'PHP' started by ghadacr, Jun 27, 2007.

  1. #1
    Morning all,

    I got a script that outputs all records from a database, beside each record there is a checkbox....When i select the record from the results by pressing the checkbox it passes to another script. That script will delete the record from the database when it recieves the ID from, the checkbox...

    Thats fine, the problem is when i select more than one record to delete it will only delete one record not the selected records..
    So basically what i want the script to do is to be able to delete more than one record at a time.. Cheers all help apperciated.

    Here is the php script in question:

    
    <?PHP include 'opendb.php'; ?> 
    
    
    <?PHP include 'header.php'; ?> 
    
    <?php 
    
    
    $FlightID = $_POST['FlightID']; 
      
    $status = "OK"; // setting the flag for form validation 
    $msg=""; // error message string is blank 
    
    
    // Now let us check if name is entered or not 
    if(strlen($FlightID) < 1 ){ // if name is less than two char length 
    $msg .="<center>Please select a flight</center><BR>"; 
    $status="NOT OK"; 
    } 
    
    
    if($status<>"OK"){ // if form validation is not passed 
    echo "<BR><BR>"; 
    echo $msg. "<br><center><input type='button' value='Retry' onClick='history.go(-1)'></center><br><br><br>"; 
    
    }else{ 
    
    
    
    $query="DELETE Flights WHERE FlightID='$FlightID '"; 
    
    mssql_query($query); 
    
    
    echo "Records Updated<p><input type='button' value='Back' onClick='history.go(-1)'>"; 
    } 
    mssql_close(); 
    ?> 
    
    <?PHP include 'footer.php'; ?>
    
    PHP:
     
    ghadacr, Jun 27, 2007 IP
  2. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    What is $_POST['FlightID']? Is it for checkboxes?, If so please rename it as FlightID[] on the previous page.And change the delete query as below:

    if(isset($_POST['FlightID']))
    {
    foreach($_POST['FlightID'] as $k=>$k1)
    {
    mysql_query("DELETE FROM Flights WHERE FlightID=$k1");
    }

    }
     
    tamilsoft, Jun 27, 2007 IP
  3. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the response,

    I did the changes you requested but i'm still only able to delete one record at a time...

    Here is the feeder scipt where the results from the database are outputed:

    
    <?PHP include 'opendb.php'; ?>
    
    
    <?PHP include 'header.php'; ?>
    <?PHP
    
    //$outboundflightno = $_GET['outboundflightno'];
    
    //$checkbox;
    $sql="SELECT * FROM Flights"; 
    $result=mssql_query($sql);
    
    $count=mssql_num_rows($result);
    
    if ($count == 0)
      {
    echo "<p>Sorry, your search returned no results</p><br><input type='button' value='Retry' onClick='history.go(-1)'>";
    
    } else {
    
    ?>
    <table width="585" height="160" border="0" cellpadding="0" cellspacing="1"> 
    <tr>
    <td><form name="form1" method="post" action="flightdelete.php">
            <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
              <tr> 
                <td bgcolor="#FFFFFF">&nbsp;</td>
                <td colspan="14" bgcolor="#FFFFFF"><strong>Delete Users</strong> </td>
              </tr>
              <tr> 
                <td align="center" bgcolor="#FFFFFF">#</td>
                <td align="center" bgcolor="#FFFFFF"><strong>FlightID</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong><strong>Outbound flight 
                  no</strong></strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Return flight no</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Group name</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Cost per adult</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Cost per child</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>From</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>To</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Outbound date</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Outbound time</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Return Date</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Return time</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Number of seats</strong></td>
                <td align="center" bgcolor="#FFFFFF"><strong>Notes</strong></td>
              </tr>
              <?php
    while($rows=mssql_fetch_array($result)){
    ?>
              <tr> 
                <td align="center" bgcolor="#FFFFFF"><input name="FlightID[]" type="checkbox" id="FlightID" value="<?php echo $rows['FlightID']; ?>"></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['FlightID']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['OutBoundFlightNumber']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['InBoundFlightNumber']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['GroupName']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['AdultCost']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['ChildCost']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['FlightFrom']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['FlightTo']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['OutBoundDate']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['OutBoundTime']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['InBoundDate']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['InBoundTime']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['NumberOfSeatsLeft']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['Notes']; ?></td>
              </tr>
              <?php
    }
    ?>
              <tr> 
                <td colspan="15" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete flight" onClick="return confirm(
      'Are you sure you want to delete this flight?');"></td>
              </tr>
              <?php
    		 }
    mssql_close();
    ?>
            </table>
    </form>
    </td>
    </tr>
    </table> 
    
    
    PHP:
    and it comes into this script to be deleted:

    
    <?PHP include 'opendb.php'; ?>
    
    
    <?PHP include 'header.php'; ?>
    
    <?php
    
    $FlightID = $_POST['FlightID']
     
    $status = "OK"; // setting the flag for form validation
    $msg=""; // error message string is blank
    
    
    // Now let us check if name is entered or not
    if(strlen($FlightID) < 1 ){ // if name is less than two char length
    $msg .="<center>Please select a flight</center><BR>";
    $status="NOT OK";
    }
    
    
    if($status<>"OK"){ // if form validation is not passed
    echo "<BR><BR>";
    echo $msg. "<br><center><input type='button' value='Retry' onClick='history.go(-1)'></center><br><br><br>";
    
    }else{
    
    if(isset($_POST['FlightID']))
    {
    foreach($_POST['FlightID'] as $k=>$k1)
    {
    
    
    $query="DELETE Flights WHERE FlightID=$k1"; 
    }
    
    }
    
    
    mssql_query($query);
    
    
    echo "Records Updated<p><input type='button' value='Back' onClick='history.go(-1)'>";
     }
    mssql_close();
    ?>
    
    <?PHP include 'footer.php'; ?>
    
    PHP:
     
    ghadacr, Jun 27, 2007 IP
  4. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks got it working


    mssql_query($query);

    put it within the loop....
     
    ghadacr, Jun 27, 2007 IP