0

I'm trying to create a delete option for a table which is directly getting data from the mysql database. I have a function to display the table and want to add 'delete' button with each category. Somehow I get Undefined index for 'cat_id' which is 'category id'according to my table.

this is the function that i am using function dispcategories() { include ('connection.php');

    $select = mysqli_query($con, "SELECT * FROM categories");
    echo '';

    while ($row = mysqli_fetch_assoc($select)) {
        echo "<table class='category-table'>";
        echo "<tr><td class='main-category' colspan='2'>".$row['category_title']."</td></tr>
              <tr><td><a href='deletecategory.php?id=".$row['cat_id'].";? >'>delete</a></td></tr>;"; 
          dispsubcategories($row['cat_id']);

        echo "</table>";
    }
}

and this is what i have on my delete page

$id = $_GET['cat_id'];
$del = mysqli_query($con, "DELETE FROM `categories` WHERE id='$id'");
Daniel Luna
  • 341
  • 2
  • 9

2 Answers2

0

Remove ;?> from href='deletecategory.php?id=".$row['cat_id'].";?>' and also you don't need semi-colon here </tr>;

Then your url is deletecategory.php?id=xx so you should have $_GET['id'] instead of $_GET['cat_id']

Astrea
  • 148
  • 9
-1

Try to modify your code as follows

$select = mysqli_query($con, "SELECT category_title, cat_id FROM categories");

    echo "<table class='category-table'>";

while ($row = mysqli_fetch_assoc($select)) {
    if(!isset($row['cat_id'])
         continue;

    echo "<tr>
              <td class='main-category' colspan='2'>".$row['category_title']."</td>
              <td><a href='deletecategory.php?id=".$row['cat_id']."'>delete</a></td>
         </tr>;"; 
      // dispsubcategories($row['cat_id']); // I don't know the use of this but try to comment it

}
    echo "</table>";

Regards

Astrea
  • 148
  • 9
Daniel Luna
  • 341
  • 2
  • 9