Page 1 of 1
Delete from checkboxes not working
Posted: Wed Jun 27, 2007 4:21 am
by ghadacr
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:
Code: Select all
<?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'; ?>
Posted: Wed Jun 27, 2007 6:01 am
by feyd
Name the field correctly i.e. "foo[]" The important part being the brackets as PHP will create an array automatically. It will be accessed by $_POST['foo']. You will need to validate each value given, escape it properly (so it isn't susceptible to SQL injection) and finally create a good query. We often suggest to modify a flag in the record instead of actually deleting it.
Posted: Wed Jun 27, 2007 6:10 am
by ghadacr
Ok this is where the results comeout from the database:
I have changed name="FlightID" to name="FlightID[]"
Code: Select all
<?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"> </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>
Then i changed the delete scipt: but still not able to delete more than one record....
Code: Select all
<?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'; ?>
Posted: Wed Jun 27, 2007 6:15 am
by volka
if(isset($_POST['FlightID']))
{
foreach($_POST['FlightID'] as $k=>$k1)
{
$query="DELETE Flights WHERE FlightID=$k1";
}
}
mssql_query($query);
mysql_query is called only once, after foreach is completed.
Posted: Wed Jun 27, 2007 6:24 am
by ghadacr
Thanks volka
mssql_query($query);
is know within the loop...
works...........cheers