Creating a complete blog (CRUD) using MySQL and PHP – Part 2

In theprevious part, we created a Post Class which will handle our article request and perform CRUD operation on it. In this tutorial, we are going to use the same class and will insert, update and delete articles.

We are going to make forms for insert and update article, create a view for article and a page where all the articles will be listed and can be deleted. I will be using bootstrap for styling. Let’s get started:

Header & Footer For View

First we are going to create a header and a footer for our pages. Now in the same folder, create a new file header.php and paste the following code in it.

Insert A New Post

Now lets create a form which will insert a new post into database and connect it with Post Class. Before creating form create a new folder in the same directory and name it postimages in order to save all the images in it. Now create a new file and name it index.php and post the following code in it.

<?php
include 'header.php';
echo 'Posted';
include 'footer.php';
?>

In this file we will redirect the user when the post is published successfully. Now lets create a new file creatpost.php and paste the following code in it.

What we did in this code is that we get the ID of the post and then call our getarticle() method and view the article on the browser.

Give it a try by running the above file and pass your postid in it by url and you will get a view of your post

Now that we have viewed our post, let’s update it.

Updating A Post

Now let’s edit our post. We will get the id of the post and paste it to our getarticle() method to get the previous article and view it in the editor to use. Then when clicked on update the article we will call updatearticle() method.

Now create a new file updatepost.php in the same directory and paste the following code:

As I’ve said before, we get the postid, update it and then we redirect it to postview if it is successfully updated. Give it a try!

Delete A Post

Deleting a post is quite easy. We just get the postid, paste it to our deletearticle() method and delete the post. Now create a new file in the the same directory and name it delete.php and paste the following code in it:

Conclusion

This is the 2nd and last part of the tutorial series in which we used our postclass to perform crud operations on our blog. All the codes can be found in the github repository . Fork it and feel free to add more things in it or clone it to use it in your projects.