I want to select a checkbox and delete the selected record i tried the below code but not working can anyone help me out in this

 <?php

$host="localhost"; // Host name 
$username="root"; // Mysql username
$password=""; // Mysql password 
$db_name="data"; // Database name
$tbl_name="register"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

 echo "<table width='800' border='1' cellspacing='0' cellpadding='3'>";
 echo "<tr >";
  echo "<td width='10%'></td>";
 echo "<td width='10%'>Id</td>";
echo "<td width='30%''>Firstname</td>";
echo "<td width='30%'>Email</td>";
echo "<td width=30%'>Password</td>";
echo "<td width='30%'>MobileNO</td>";
echo "<td width='30%'>DOB</td>";
echo "<td width='30%'>EmployeeId</td>";
echo "<td width='30%'>EmployeePosition</td>";
echo "<td width='30%'>IPadress</td>";
echo"<td width='30%'>MACaddress</td>";
echo "<td width='30%'>SecurityQuestion</td>";
echo "<td width='30%'>SecurityAnswer</td>";



// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){


echo "<tr>";
echo "<td width='10%'><form action='"$PHP_SELF"' method='POST'><input type='checkbox' name='chkbox1[]'/></form></td>";

echo "<td width='10%'>" .$rows['id']. "</td>";
echo "<td width='30%'>" .$rows['Name']. "</td>";
echo "<td width='30%'>" .$rows['Email']. "</td>";
echo "<td width='30%'>".$rows['Password']."</td>";
echo "<td width='30%'>".$rows['MobileNo']."</td>";
echo "<td width='30%'>".$rows['Dob']."</td>";
echo "<td width='30%'>".$rows['EmployeeId']."</td>";
echo "<td width='30%'>".$rows['EmployeePosition']."</td>";
echo "<td width='30%'>".$rows['IPaddress']."</td>";
echo "<td width='30%'>".$rows['MACaddress']."</td>";
echo "<td width='30%'>".$rows['SecurityQuestion']."</td>";
echo "<td width='30%'>".$rows['SecurityAnswer']."</td>";
echo "<td width='30%'><form action='"$PHP_SELF"' method='POST'><input type='button' name='delete' value='Delete' /></form></td>";
echo "</tr>";
}


echo "</table>";






// close while loop 



// Check if delete button active, start this 

if(isset($_POST['delete']))
{
   chkbox1 = $rows['id'];

for($i=0;$i<count($chkbox1);$i++){

$del_id = $chkbox1[$i];
$sql = "DELETE FROM register WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display_register.php\">";
}
 }








// close MySQL connection 
mysql_close();
?>

Hi,
try this in your PHP code:
- first move out of the while the echo with <form> definition
- second, the checkbox

 echo "<input type='checkbox' name='chkbox1[]' value = '".$rows['id'] ."' />";
  • third - how make the delete

    if(isset($_POST['delete']))
    {
    $chkbox1 = $_POST[chkbox1];
    }
    foreach($chkbox1 as $del_id)
    {
    $sql = "DELETE FROM register WHERE id='$del_id'";
    $result = mysql_query($sql);
    }

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.