Before I start, I just wanted to make it clear that I have never used PHP before, therefore none of the code in this post is mine. It is all from tutorial sites that I am trying to get to work. My mistakes will probably be incredibly basic.

Anyway, I'm using a MySQL database to store the records that are entered using a HTML form. These records are then displayed on another page. This bit works fine. The problem I am having is with another script I am trying to use to enable the user to delete a row/record using a checkbox and then a delete button. The form submits and says user deleted successfully, but when I go back to my main page with the list of users on it the record is still there. Help would be appreciated.

The codes are as follows:

<html>  
<head>  
<title>ShotDev.Com Tutorial</title>  
</head>  
<body>  
<script language="JavaScript">  
function onDelete()  
{  
if(confirm('Do you want to delete ?')==true)  
{  
return true;  
}  
else  
{  
return false;  
}  
}  
</script>  
<form name="frmMain" action="php_mysql_checkbox2.php" method="post" OnSubmit="return onDelete();">  
<?  
$objConnect = mysql_connect("mysql2.000webhost.com","a1831345_listete","liste123") or die(mysql_error());  
$objDB = mysql_select_db("a1831345_liste");  
$strSQL = "SELECT * FROM personnel_army";  
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");  
?>  
<table width="600" border="1">  
<tr>  
<th width="91"> <div align="center">Name </div></th>  
<th width="98"> <div align="center">Billet </div></th>  
<th width="198"> <div align="center">Tag </div></th>  
<th width="97"> <div align="center">AITs </div></th>  
<th width="59"> <div align="center">Date </div></th>  
<th width="71"> <div align="center">Medals </div></th>  
<th width="30"> <div align="center">Delete </div></th>  
</tr>  
<?  
while($objResult = mysql_fetch_array($objQuery))  
{  
?>  
<tr>  
<td><div align="center"><?=$objResult["Name"];?></div></td>  
<td><?=$objResult["Billet"];?></td>
<td><?=$objResult["Tag"];?></td>  
<td><div align="center"><?=$objResult["AITs"];?></div></td>  
<td align="right"><?=$objResult["Date"];?></td>  
<td align="right"><?=$objResult["Medals"];?></td>  
<td align="center"><input type="checkbox" name="chkDel[]" value="<?=$objResult["CustomerID"];?>"></td>  
</tr>  
<?  
}  
?>  
</table>  
<?  
mysql_close($objConnect);  
?>  
<input type="submit" name="btnDelete" value="Delete">  
</form>  
</body>  
</html>  
Code 2:
<html>  
<head>  
<title>ShotDev.Com Tutorial</title>  
</head>  
<body>  
<?  
$objConnect = mysql_connect("mysql2.000webhost.com","a1831345_listete","liste123") or die(mysql_error());  
$objDB = mysql_select_db("a1831345_liste");  

for($i=0;$i<count($_POST["chkDel"]);$i++)  
{  
if($_POST["chkDel"][$i] != "") 
{
$strSQL = "DELETE FROM personnel_army";  
$strSQL .="WHERE Name = '".$_POST["chkDel"][$i]."' ";  
$objQuery = mysql_query($strSQL);  
}  
}  

echo "Record Deleted.";  

mysql_close($objConnect);  
?>  
</body>  
</html>  


Thank you everyone!

Recommended Answers

All 2 Replies

Do not ever post your mysql_connect passwords or usernames. People can hack into your database.

First check in your generated html code whether checkboxes have intended names (chkDel[] and values (the ID of the customer). You can do that by looking at the source in your browser (right button and View Page Source in Firefox).

Then check whether the $_POST contains appropriate values in code 2. You can do this by inserting the following command on say line 9:

die(print_r($_POST, 1));

This will print the content of $_POST values which should contain an array of values of checked checkboxes (and a value for submit button).

But first things first: immediately change your credentials for database access.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.