Ariel
Ariel

Reputation: 79

editing the category of the posts in php mysql

I have categories in my blog and i want the result if you edit the category name, then category names of the posts will change as well

if(isset($_POST['update_cat'])){
    $cat_id = $_POST['cat_id'];
    $cat_name = $_POST['cat_name'];

    $var=mysqli_query($con,"UPDATE category SET  category_name = '$cat_name' WHERE category_id = $cat_id");
    if ($var) {
        $var2=mysqli_query($con,"UPDATE post INNER JOIN category on post.postCategory=category.category_name SET  postCategory = '$cat_name' WHERE postCategory = $cat_name");  
    }

Upvotes: 0

Views: 402

Answers (1)

Penguine
Penguine

Reputation: 509

There are two options for this:

1) You should create two database tables. One for category and one for posts, such that the ID(primary key) of the Categories table should be referenced from category_id(foreign key) of the posts table. The category name(which is in the category table), whenver changed, all posts that refer that corresponding ID, shall also bear the new name.

2) If you are maintaining a category_name as a column in the posts table, then you should use an after update trigger on the category table, such that whenever a category name changes, the trigger changes all the category_names in the Posts table.

The first option is the correct way of proceeding as the second might bring a lot of inconsistency in your records.

Upvotes: 1

Related Questions