Help with deleting from sql database
Posted: Thu May 20, 2010 3:22 pm
i have this code below it shows the info i need but when i click a checkbox to delete the selected row and then click delete it just refreshes the page.
does anyone know what i need to do to fix this.
Thanks
does anyone know what i need to do to fix this.
Thanks
Code: Select all
<?php
$host="Host"; // Host name
$username="user"; // Mysql username
$password="pass"; // Mysql password
$db_name="Targets"; // Database name
$tbl_name="Target"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="delete" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" align="center" bgcolor="#FFFFFF"><strong>Targets</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Target</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Comment</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['ID']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Target']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Comment']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_ID = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE ID='$del_ID'";
$result = mysql_query($sql);
}
// if successful redirect to delete.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>