I cannot delete the records after pressing the delete button. What is the fatal error? Thanks!


admin.php

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // 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);
?>


<form id="form" name="form" method="post" action="deleteApplication.php"><table border='1' cellspacing='0' width='612'>
<tr>
<th bgcolor='green'><font color='white'>#</font></th>
<th bgcolor='green'><font color='white'>Id</font></th>
<th bgcolor='green'><font color='white'>Name</font></th>
<th bgcolor='green'><font color='white'>Lastname</font></th>
<th bgcolor='green'><font color='white'>Email</font></th>
</tr>
               
<?php
$i = 0; 

$number = 0;
while($row = mysql_fetch_array($result)){

$number++;

?>
   
<?php
$i++;

if($i%2)
{
$bg_color = "#EEEEEE";
}
else {
$bg_color = "#E0E0E0";
}
?> 
   
   <tr bgcolor='". $bg_color ."'>
   <td><center><Strong><font color='red'><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>">
   </font></Strong>
   </center></td>
   <td><center><Strong><font color='red'><?php echo $row['id']; ?></font></Strong></center></td>
  <td><center><Strong><?php echo $row['name']; ?></font></Strong></center></td>
  <td><center><Strong><?php echo $row['lastname']; ?></Strong></center></td>
  <td><center><Strong><?php echo $row['email'];  ?></Strong></center></td>
  </tr>
<?php } ?>
            </table>
            <div class="select"><strong>Other Pages: </strong>
               <select>
                  <option>1</option>
               </select>
           </div>
             
               <label>
                 <input name="delete" type="submit" id="delete" value="Delete">


                </label></form>

deleteApplication.php

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // 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);

   
      if(isset($_POST['delete'])) {
   
      foreach($_POST['checkbox'] as $id){
   
      mysql_query("DELETE from $tbl_name WHERE id ='.$id.'");
   }
        header('Location: admin.php');
      
    }
?>
Member Avatar for Zagga

Hi cliffcc,

try adding something like or die('Error - Could not perform the query: ' . mysql_error()); to the end of your SQL statements to see where they are failing.

Are you getting any other error messages?

I have erased the dots, but i still cannot delete the records. And i guess query failure or have errors in checkbox. I guess I cannot get the value (id) of checkbox. But i cannot find out the error. Anyone can help? Thanks!

Member Avatar for Zagga

Hi again,

Do you get any error messages? Does the querie give an error (with the 'or die' code added)?

Just scanning through I see that in admin.php on line 49, you are echoing $rows['id'] . This should just be $row['id'] .

try this one:
mysql_query("DELETE from " . $tbl_name . " WHERE id = " . $id . "");

Thanks all! I can delete the records successfully!

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.