0

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)....

alexwlchan
  • 5,699
  • 7
  • 38
  • 49
Divyesh Jesadiya
  • 1,105
  • 4
  • 30
  • 68
  • what is your `cat_id` field in the DB? an `integer` or a `varchar`? And make this change to check for **mysql errors**: `$sql=mysql_query("DELETE FROM category_tbl WHERE cat_id='".$id."'") OR die(mysql_error());` – Cliff Burton Apr 25 '15 at 11:24
  • 2
    Is the row deleted in the database? And you have an sql injection problem. – jeroen Apr 25 '15 at 11:26
  • no,it's not deleted from database...and my cat_id is integer... – Divyesh Jesadiya Apr 25 '15 at 11:43
  • @Cliff Burton...after using die it is showing this...Cannot delete or update a parent row: a foreign key constraint fails (`db_stationery`.`product_tbl`, CONSTRAINT `product_tbl_ibfk_1` FOREIGN KEY (`cat_id`) REFERENCES `category_tbl` (`cat_id`)) – Divyesh Jesadiya Apr 25 '15 at 11:47
  • 1
    It means that you are trying to delete a table row which is referenced to a parent table with a `ON DELETE NO ACTION` or `ON DELETE RESTRICT`. You should revise your DB structure if you want to perform this action – Cliff Burton Apr 25 '15 at 11:52
  • Is this something that you are developing right now or is it something that you are stuck with? If the former, then you should definitely not use mysql_! – v010dya Apr 25 '15 at 12:36

1 Answers1

1

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

Community
  • 1
  • 1
Bhavesh Rangani
  • 1,490
  • 12
  • 23