Divyesh Jesadiya
Divyesh Jesadiya

Reputation: 957

Why is my delete operation not performing?

In my project, I want a delete operation for a particular id when I click on the delete button, but it isn't performing. Here's my code for displaying the table:

<table class="listing" cellpadding="0" cellspacing="0">
      <tr>
        <th class="first"><center>Id</center></th>
        <th>Category Name</th>
        <th>Status</th>
        <th>Edit</th>
        <th class="last">Delete</th>
      </tr>
  <?php
   include('config.php');
   $sql="select * from category_tbl";
   $result=mysql_query($sql);
   while ($row=mysql_fetch_array($result)) {
if($row['cat_status'] == 0) {
  $im='<a href="category.php?false='.$row["cat_id"].'"><img src="../images/red.jpg" height="28" width="28"></a>';
    }
    else{
        $im='<a href="category.php?true='.$row["cat_id"].'"><img src="../images/green.jpg" height="30" width="30"></a>';
    }
    if (isset($_REQUEST['false'])) {
      $updt=mysql_query("update category_tbl set cat_status=1 where cat_id='".$_REQUEST['false']."'");
      header('location:category.php');
    }
    if (isset($_REQUEST['true'])) {
      $updt=mysql_query("update category_tbl set cat_status=0 where cat_id='".$_REQUEST['true']."'");
      header('location:category.php');
    }
    ?>
    <tr>
        <td><strong><?php echo $row['cat_id'];?></strong></td>
        <td><strong><?php echo $row['cat_name'];?></strong></td>
        <td><?php echo $im;?></td>
        <td><a href="update_cat.php?id=<?php echo $row['cat_id'];?>"><img src="../images/edit.jpg" height="30" width="60" value=<?php echo $row['cat_id'];?>></a></td>
        <td><a href="delete_cat.php?id=<?php echo $row['cat_id'];?>"><img src="../images/delete1.jpg" height="30" width="60" value=<?php echo $row['cat_id'];?>></a></td>
    </tr>
    <?php
  }
  ?>
  </table>

The edit operation is working, but delete is not. This is my code for delete:

<?php
include("config.php");
$id=$_REQUEST['id'];
$sql=mysql_query("DELETE FROM category_tbl WHERE cat_id='".$id."'");
if ($sql) {
    header("location:category.php");
}   
?>

After execution it stays on delete_cat.php?id=(passed id)....

Upvotes: 0

Views: 141

Answers (1)

Bhavesh Rangani
Bhavesh Rangani

Reputation: 1510

i think you need to remove your foreign key from sub table or second option is you need to alter your query and set cascade like this.

ON DELETE CASCADE

for more help visit this link

Upvotes: 1

Related Questions