{"id":14463,"date":"2016-08-22T12:15:18","date_gmt":"2016-08-22T09:15:18","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=14463"},"modified":"2016-08-18T22:59:31","modified_gmt":"2016-08-18T19:59:31","slug":"creating-complete-blog-crud-using-mysql-and-php-part2","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/","title":{"rendered":"Creating a complete blog (CRUD) using MySQL and PHP &#8211; Part 2"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-php\/\">previous part<\/a>, 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.<\/p>\n<p>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\u2019s get started:<\/p>\n<h2>Header &amp; Footer For View<\/h2>\n<p>First we are going to create a header and a footer for our pages. Now in the same folder, create a new file <strong>header.php<\/strong> and paste the following code in it.<\/p>\n<pre class=\"brush:php; wrap-lines:false\">&lt;!DOCTYPE html PUBLIC \"-#W3C#DTD XHTML 1.0 Transitional#EN\" \"http:#www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\r\n\r\n&lt;!--\r\nTo change this license header, choose License Headers in Project Properties.\r\nTo change this template file, choose Tools | Templates\r\nand open the template in the editor.\r\n--&gt;\r\n&lt;html xmlns=\"http:#www.w3.org\/1999\/xhtml\"&gt;\r\n    &lt;head&gt;\r\n        &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\"&gt;\r\n         &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\r\n        &lt;title&gt;Blog System&lt;\/title&gt;\r\n        &lt;!-- Latest compiled and minified CSS --&gt;\r\n&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz\/K68vbdEjh4u\" crossorigin=\"anonymous\"&gt;\r\n\r\n&lt;!-- Optional theme --&gt;\r\n&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap-theme.min.css\" integrity=\"sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl\/Sp\" crossorigin=\"anonymous\"&gt;\r\n        \r\n        \r\n    &lt;\/head&gt;\r\n    &lt;body class=\"container-fluid\"&gt;\r\n            &lt;div class=\"container-fluid\"&gt;<\/pre>\n<p>Now create a new file <strong>footer.php<\/strong> and paste the following code in it.<\/p>\n<pre class=\"brush:php\">&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.1.0.min.js\"&gt;&lt;\/script&gt;\r\n&lt;!-- Latest compiled and minified JavaScript --&gt;\r\n&lt;script src=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/js\/bootstrap.min.js\" integrity=\"sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\r\n\r\n\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h2>Insert A New Post<\/h2>\n<p>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 <strong>postimages<\/strong> in order to save all the images in it. Now create a new file and name it <strong>index.php<\/strong> and post the following code in it.<\/p>\n<pre class=\"brush:php\">&lt;?php\r\ninclude 'header.php';\r\n\r\necho 'Posted';\r\n\r\ninclude 'footer.php';\r\n\r\n?&gt;\r\n<\/pre>\n<p>In this file we will redirect the user when the post is published successfully. Now lets create a new file <strong>creatpost.php<\/strong> and paste the following code in it.<\/p>\n<pre class=\"brush:php; wrap-lines:false\">&lt;?php\r\n\r\ninclude 'header.php';\r\ninclude 'postclass.php';\r\n        \r\n$crud   = new Post();\r\n$errors = [\r\n    'article_name'     =&gt; null,\r\n    'article_category' =&gt; null,\r\n    'article_content'  =&gt; null,\r\n    'article_img'      =&gt; null,\r\n    'form'             =&gt; null,\r\n];\r\n\r\n$conne         = '';\r\n$form          = true;\r\n$target_dir    = \"postimages\/\";\r\n$target_file   = null;\r\n$imageFileType = null;\r\nif (!empty($_POST)) {\r\n\r\n    $target_file   = $target_dir . basename($_FILES[\"article_img\"][\"name\"]);\r\n    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);\r\n    $check         = getimagesize($_FILES[\"article_img\"][\"tmp_name\"]);\r\n    if (empty($_POST['a_name'])) {\r\n        $errors['article_name'] = \"Please Enter Your Title\";\r\n        $form                   = false;\r\n    }\r\n    if ($check === false) {\r\n        $errors['article_img'] = 'Image is required';\r\n        $form                  = false;\r\n    } elseif ($imageFileType != \"jpg\" &amp;&amp; $imageFileType != \"png\" &amp;&amp; $imageFileType != \"jpeg\" &amp;&amp; $imageFileType != \"gif\") {\r\n        $errors['article_img'] = 'Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.';\r\n        $form                  = false;\r\n    }\r\n\r\n    if (empty($_POST['article_content'])) {\r\n        $errors['article_content'] = \"Please Fill Your Content\";\r\n        $form                      = false;\r\n    }\r\n\r\n    if ($form) {\r\n\r\n        $articlename = $_POST['a_name'];\r\n        $articlecont = $_POST['article_content'];\r\n        \r\n        $image       = rand(1, 100);\r\n        $imgname     = $image . \".\" . $imageFileType;\r\n        $result      = $crud-&gt;insertpost($articlename, $articlecont, $imgname);\r\n\r\n        if ($result == 'true') {\r\n            move_uploaded_file($_FILES[\"article_img\"][\"tmp_name\"], $target_dir . $imgname);\r\n            header('Location: index.php');\r\n            return;\r\n        } else {\r\n            $errors['form'] = $result;\r\n        }\r\n\r\n    }\r\n\r\n} else {\r\n    $errors['form'] = \"Kindly Fill All the Fields\";\r\n}\r\n\r\n?&gt;\r\n&lt;!--&lt;script type=\"text\/javascript\" src=\"tinymce\/tinymce.min.js\"&gt;&lt;\/script&gt;--&gt;\r\n&lt;script src=\"http:\/\/js.nicedit.com\/nicEdit-latest.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\r\n    &lt;script type=\"text\/javascript\"&gt;\r\n        \/\/ tinymce.init({\r\n        \/\/     selector: \"#article_content\"\r\n        \/\/ });\r\n\r\nbkLib.onDomLoaded(function() {\r\n  new nicEditor().panelInstance('article_content');\r\n\r\n});\r\n    &lt;\/script&gt;\r\n\r\n&lt;form class=\"form-horizontal\" method=\"post\" enctype=\"multipart\/form-data\" action=\"&lt;?php echo htmlspecialchars($_SERVER[\"PHP_SELF\"]); ?&gt;\"&gt;\r\n&lt;fieldset&gt;\r\n\r\n&lt;!-- Form Name --&gt;\r\n&lt;legend&gt;Create Post&lt;\/legend&gt;\r\n\r\n&lt;!-- Text input--&gt;\r\n&lt;div class=\"form-group col-md-10\"&gt;\r\n  &lt;label class=\"control-label\" for=\"a_name\"&gt;Create Post&lt;\/label&gt;\r\n\r\n  &lt;input id=\"a_name\" name=\"a_name\" type=\"text\" placeholder=\"Enter Title For Your Article\" class=\"form-control input-md\"  value=\"&lt;?php if (isset($_POST['a_name'])) {echo $_POST['a_name'];}\r\n?&gt;\" required&gt;\r\n    &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['article_name'] ?&gt;&lt;\/p&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;!-- File Button --&gt;\r\n&lt;div class=\"form-group col-md-10\"&gt;\r\n  &lt;label class=\"control-label\" for=\"article_img\"&gt;Upload Image&lt;\/label&gt;\r\n\r\n    &lt;input id=\"article_img\" name=\"article_img\" class=\"input-file\" type=\"file\" required&gt;\r\n\r\n     &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['article_img'] ?&gt;&lt;\/p&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;!-- Textarea --&gt;\r\n&lt;div class=\"form-group col-md-10\" &gt;\r\n  &lt;label class=\"control-label\" for=\"article_content\"&gt;Content&lt;\/label&gt;\r\n\r\n    &lt;textarea class=\"form-control\" id=\"article_content\" name=\"article_content\" rows=\"13\"&gt;&lt;?php if (isset($_POST['article_content'])) {echo $_POST['article_content'];}\r\n?&gt;&lt;\/textarea&gt;\r\n    &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['article_content'] ?&gt;&lt;\/p&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;!-- Button --&gt;\r\n&lt;div class=\"form-group col-md-10\"&gt;\r\n  &lt;label class=\"control-label\" for=\"submit\"&gt;&lt;\/label&gt;\r\n\r\n    &lt;button id=\"submit\" name=\"submit\" type=\"submit\" class=\"btn btn-success\"&gt;Publish&lt;\/button&gt;\r\n    &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['form'] ?&gt;&lt;\/p&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;\/fieldset&gt;\r\n&lt;\/form&gt;\r\n\r\n\r\n&lt;?php include 'footer.php';?&gt;<\/pre>\n<p>In the above code we have created a form and also done its validation so that it doesn\u2019t send empty data in database. Then if the data is submitted to database we will upload its image on our server.<\/p>\n<p>Now run it on your browser by going to your root folder and then to createpost.php.It will look like this:<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image00.png\"><img decoding=\"async\" class=\"aligncenter wp-image-14464\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image00.png\" alt=\"image00\" width=\"860\" height=\"496\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image00.png 1096w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image00-300x173.png 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image00-768x443.png 768w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image00-1024x590.png 1024w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<p>Try sending a post, if it has no error you will be transferred to the succeed page and you can also check on your database to verify that your post is edited.<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image01.png\"><img decoding=\"async\" class=\"aligncenter wp-image-14465\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image01.png\" alt=\"image01\" width=\"860\" height=\"67\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image01.png 933w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image01-300x23.png 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image01-768x60.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<p>Now let\u2019s create a page where these posts will be listed.<\/p>\n<h2>View Publish Post<\/h2>\n<p>Now go to the file in your directory and name it <strong>postview.php<\/strong> and paste the following code in it.<\/p>\n<pre class=\"brush:php; wrap-lines:false\">&lt;?php\r\ninclude 'header.php';\r\ninclude 'postclass.php';\r\n\r\n$postid = $_GET['postid'];\r\n\r\n$article = new Post();\r\n$result  = $article-&gt;getarticle($postid);\r\n\r\n?&gt;\r\n&lt;div class='row'&gt;\r\n&lt;?php\r\nwhile ($row = $result-&gt;fetch_assoc()) {\r\n    ?&gt;\r\n  &lt;div class=\"panel panel-default\"&gt;\r\n  &lt;div class=\"panel-heading back\"&gt;\r\n    &lt;h2&gt; &lt;?php echo $row['article_name'];?&gt; &lt;\/h2&gt;\r\n    &lt;h5&gt;&lt;span class=\"glyphicon glyphicon-time\"&gt;&lt;\/span&gt; Posted On &lt;?php echo $row['dates'];?&gt;&lt;\/h5&gt;\r\n      \r\n  &lt;\/div&gt;\r\n  &lt;div class=\"panel-body\"&gt;\r\n    &lt;div class=\"col-md-3 pull-right\"&gt;\r\n    &lt;img class='img-responsive img-rounded center' src=\"postimages\/&lt;?php echo $row['img'];?&gt;\"&gt;\r\n    &lt;\/div&gt;\r\n   &lt;?php echo $row['article_content'];?&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;?php\r\n}\r\n?&gt;\r\n&lt;\/div&gt;\r\n\r\n\r\n  &lt;?php\r\ninclude 'footer.php';\r\n?&gt;\r\n<\/pre>\n<p>What we did in this code is that we get the ID of the post and then call our <strong>getarticle()<\/strong> method and view the article on the browser.<\/p>\n<p>Give it a try by running the above file and pass your <strong>postid<\/strong> in it by url and you will get a view of your post<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image02.png\"><img decoding=\"async\" class=\"aligncenter wp-image-14466\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image02.png\" alt=\"image02\" width=\"860\" height=\"282\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image02.png 1339w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image02-300x98.png 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image02-768x252.png 768w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/08\/image02-1024x336.png 1024w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<p>Now that we have viewed our post, let\u2019s update it.<\/p>\n<h2>Updating A Post<\/h2>\n<p>Now let\u2019s edit our post. We will get the id of the post and paste it to our <strong>getarticle()<\/strong> method to get the previous article and view it in the editor to use. Then when clicked on update the article we will call <strong>updatearticle()<\/strong> method.<\/p>\n<p>Now create a new file <strong>updatepost.php<\/strong> in the same directory and paste the following code:<\/p>\n<pre class=\"brush:php; wrap-lines:false\">&lt;?php\r\ninclude 'header.php';\r\ninclude 'postclass.php';\r\n$user = $_SESSION[\"username\"];\r\n$aid = null;\r\n\r\nif(empty($_POST))\r\n{\r\n  $aid = $_GET['postid'];\r\n}\r\nelse{\r\n  $aid = $_POST['postid'];\r\n}\r\n\r\n\r\n$crud = new Post();\r\n$errors = [\r\n        'article_name' =&gt; null,\r\n        'article_category' =&gt; null,\r\n        'article_content' =&gt; null,\r\n        'article_img' =&gt; null,\r\n        'form' =&gt; null\r\n    ];\r\n$article = [\r\n        'a_id' =&gt; $aid,\r\n        'a_name' =&gt; null,\r\n        'a_con' =&gt; null,\r\n        'a_img' =&gt; null,\r\n        'form' =&gt; null\r\n    ];\r\n\r\n    $result = $crud-&gt;getarticle($aid);\r\n\r\n    while($row = $result-&gt;fetch_assoc())\r\n        {\r\n          $article['a_name'] = $row['article_name'];\r\n          $article['a_con'] = $row['article_content'];\r\n          $article['a_img'] = $row['img'];\r\n        }\r\n\r\n    $conne='';\r\n    $form = true;\r\n    $target_dir = \"postimages\/\";\r\n    $target_file = null;\r\n    $imageFileType = null;\r\n    $Upload = false;\r\n\r\n  \/\/  var_dump($article);\r\nif(!empty($_POST))\r\n{\t\r\n\t\r\n\t$target_file = $target_dir . basename($_FILES[\"article_img\"][\"name\"]);\r\n\t$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);\r\n  $check = false;\r\n\t$check = getimagesize($_FILES[\"article_img\"][\"tmp_name\"]);\r\n\r\n\tif($_POST['a_name'] !== $article['a_name'])\r\n\t{\r\n\t\t$article['a_name'] = $_POST['a_name'];\r\n\t\r\n\t}\r\n\tif ($check !== false)\r\n\t{\r\n  \t$Upload = true;\r\n\tif($imageFileType != \"jpg\" &amp;&amp; $imageFileType != \"png\" &amp;&amp; $imageFileType != \"jpeg\"&amp;&amp; $imageFileType != \"gif\" ) \r\n\t{\r\n\t   $errors['article_img'] = 'Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.';\r\n\t   $form =false;\r\n\t}\r\n  }\r\n\r\n\tif($_POST['article_content'] !== $article['a_con'])\r\n\t{\r\n\t\t$article['a_con'] = $_POST['article_content'];\r\n\t\t\r\n\t}\r\n\r\nif($form)\r\n{\r\n  \r\n  \r\n  if($Upload)\r\n  {\r\n            $image = rand(1,100);\r\n            $imgname = $image .\".\".$imageFileType;\r\n            $result = $crud-&gt;updatearticle($article['a_id'],$article['a_con'],$article['a_name'],$imgname);\r\n          if($result == 'true')\r\n              {\r\n                move_uploaded_file($_FILES[\"article_img\"][\"tmp_name\"], $target_dir.$imgname);\r\n                          header(\"Location: postview.php?postid=$aid\");\r\n                               return;\r\n              }\r\n          else\r\n              {\r\n                $errors['form'] = $result;\r\n              }\r\n  }\r\n  else\r\n  {\r\n          var_dump($article);\r\n          $result =  $crud-&gt;updatearticle($article['a_id'],$article['a_con'],$article['a_name'],$article['a_img']);\r\n          if($result == 'true')\r\n              {\r\n                \r\n               header(\"Location: postview.php?postid=$aid\");\r\n                               return;\r\n              }\r\n          else\r\n              {\r\n                $errors['form'] = $result;\r\n              }\r\n  }\r\n\r\n  \r\n\t\r\n}\r\n\r\n}\r\nelse\r\n{\r\n\t$errors['form'] = \"Kindly Fill All the Fields\";\r\n}\r\n\r\n\r\n?&gt;\r\n&lt;!--&lt;script type=\"text\/javascript\" src=\"tinymce\/tinymce.min.js\"&gt;&lt;\/script&gt;--&gt;\r\n&lt;script type=\"text\/javascript\" src=\"style\/js\/nicEdit.js\"&gt;&lt;\/script&gt;\r\n    &lt;script type=\"text\/javascript\"&gt;\r\n        \/\/ tinymce.init({\r\n        \/\/     selector: \"#article_content\"\r\n        \/\/ });\r\n\r\nbkLib.onDomLoaded(function() {\r\n  new nicEditor().panelInstance('article_content');\r\n  \r\n});\r\n    &lt;\/script&gt;\r\n\r\n&lt;form class=\"form-horizontal\" method=\"post\" enctype=\"multipart\/form-data\" action=\"&lt;?php echo htmlspecialchars($_SERVER[\"PHP_SELF\"]);?&gt;\"&gt;\r\n&lt;fieldset&gt;\r\n&lt;input type=\"hidden\" name=\"postid\" value=\"&lt;?php echo $_GET['postid']?&gt;\"&gt;\r\n&lt;!-- Form Name --&gt;\r\n&lt;legend&gt;Update Article&lt;\/legend&gt;\r\n\r\n&lt;!-- Text input--&gt;\r\n&lt;div class=\"form-group col-md-10\"&gt;\r\n  &lt;label class=\"control-label\" for=\"a_name\"&gt;Title&lt;\/label&gt;  \r\n  \r\n  &lt;input id=\"a_name\" name=\"a_name\" type=\"text\" placeholder=\"Enter Title For Your Article\" class=\"form-control input-md\"  value=\"&lt;?php echo $article['a_name'];  ?&gt;\" required&gt;\r\n    &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['article_name']?&gt;&lt;\/p&gt;\r\n  \r\n&lt;\/div&gt;\r\n\r\n&lt;!-- File Button --&gt; \r\n&lt;div class=\"form-group col-md-10\"&gt;\r\n  &lt;label class=\"control-label\" for=\"article_img\"&gt;Upload Image&lt;\/label&gt;\r\n  \r\n    &lt;input id=\"article_img\" name=\"article_img\" class=\"input-file\" type=\"file\" value=\"&lt;?php echo $article['a_img']?&gt;\"&gt;\r\n\r\n     &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['article_img']?&gt;&lt;\/p&gt;\r\n  \r\n&lt;\/div&gt;\r\n\r\n&lt;!-- Textarea --&gt;\r\n&lt;div class=\"form-group col-md-10\" &gt;\r\n  &lt;label class=\"control-label\" for=\"article_content\"&gt;Content&lt;\/label&gt;\r\n                      \r\n    &lt;textarea class=\"form-control\" id=\"article_content\" name=\"article_content\" rows=\"13\"&gt;&lt;?php echo $article['a_con'];  ?&gt;&lt;\/textarea&gt;\r\n    &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['article_content']?&gt;&lt;\/p&gt;\r\n  \r\n&lt;\/div&gt;\r\n\r\n&lt;!-- Button --&gt;\r\n&lt;div class=\"form-group col-md-10\"&gt;\r\n  &lt;label class=\"control-label\" for=\"submit\"&gt;&lt;\/label&gt;\r\n \r\n    &lt;button id=\"submit\" name=\"submit\" class=\"btn btn-success\"&gt;Update&lt;\/button&gt;\r\n    &lt;a href=\"articlelist\" id=\"cancel\" name=\"cancel\" class=\"btn btn-danger\"&gt;Cancel&lt;\/a&gt;\r\n    &lt;p class=\"text-danger\"&gt;&lt;?php echo $errors['form']?&gt;&lt;\/p&gt;\r\n  \r\n&lt;\/div&gt;\r\n\r\n&lt;\/fieldset&gt;\r\n&lt;\/form&gt;\r\n\r\n\r\n&lt;?php include 'footer.php';?&gt;\r\n<\/pre>\n<p>As I\u2019ve said before, we get the <strong>postid, <\/strong>update it and then we redirect it to postview if it is successfully updated. Give it a try!<\/p>\n<h2>Delete A Post<\/h2>\n<p>Deleting a post is quite easy. We just get the <strong>postid,<\/strong> paste it to our <strong>deletearticle()<\/strong> method and delete the post. Now create a new file in the the same directory and name it <strong>delete.php<\/strong> and paste the following code in it:<\/p>\n<pre class=\"brush:php\">&lt;?php \r\ninclude 'postclass.php';\r\n\r\n$ids = $_GET['postid'];\r\n\r\n$del = new Post();\r\n\r\n$result = $del-&gt;deletearticle($ids);\r\n\r\nif($result === true)\r\n{\r\n\techo \"Post Is Deleted Successfully\";\r\n}\r\nelse\r\n{\r\n\techo $result;\r\n}\r\n\r\n\r\n\r\n\r\n?&gt;<\/pre>\n<p>Give it a try and delete your post.<\/p>\n<h2>Conclusion<\/h2>\n<p>This is the 2nd and last part of the tutorial series in which we used our <strong>postclass<\/strong> to perform crud operations on our blog. All the codes can be found in the <a href=\"https:\/\/github.com\/ahmedkhan847\/postclass\">github repository<\/a>. Fork it and feel free to add more things in it or clone it to use it in your projects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous 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 &hellip;<\/p>\n","protected":false},"author":174,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[381],"class_list":["post-14463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-mysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating a complete blog (CRUD) using MySQL and PHP - Part 2 - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In the previous 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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating a complete blog (CRUD) using MySQL and PHP - Part 2 - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In the previous 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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/ahmedkhannn\" \/>\n<meta property=\"article:published_time\" content=\"2016-08-22T09:15:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ahmed Khan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ahmed0627\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ahmed Khan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/\"},\"author\":{\"name\":\"Ahmed Khan\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/39826783730ea673ddc3383fe3bcf855\"},\"headline\":\"Creating a complete blog (CRUD) using MySQL and PHP &#8211; Part 2\",\"datePublished\":\"2016-08-22T09:15:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/\"},\"wordCount\":630,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"keywords\":[\"MySQL\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/\",\"name\":\"Creating a complete blog (CRUD) using MySQL and PHP - Part 2 - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-08-22T09:15:18+00:00\",\"description\":\"In the previous 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\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Creating a complete blog (CRUD) using MySQL and PHP &#8211; Part 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/39826783730ea673ddc3383fe3bcf855\",\"name\":\"Ahmed Khan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9c0240c8a389a58b007699e24a2d7e937f6d150934678d723fb7037e3e5d5d56?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9c0240c8a389a58b007699e24a2d7e937f6d150934678d723fb7037e3e5d5d56?s=96&d=mm&r=g\",\"caption\":\"Ahmed Khan\"},\"description\":\"Ahmed Khan is the PHP Community Manager at Cloudways, a hosting company that specializes in optimized PHP hosting services. He writes about PHP, MySQL and covers different tips and tricks related to PHP. He is currently active on Cloudways and other different blogs. When he is not writing about PHP, he likes watching The Flash, Game Of Thrones and is a die-hard fan of DC Comics.\",\"sameAs\":[\"https:\/\/www.facebook.com\/ahmedkhannn\",\"https:\/\/x.com\/ahmed0627\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/ahmed-khan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating a complete blog (CRUD) using MySQL and PHP - Part 2 - Web Code Geeks - 2026","description":"In the previous 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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/","og_locale":"en_US","og_type":"article","og_title":"Creating a complete blog (CRUD) using MySQL and PHP - Part 2 - Web Code Geeks - 2026","og_description":"In the previous 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","og_url":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_author":"https:\/\/www.facebook.com\/ahmedkhannn","article_published_time":"2016-08-22T09:15:18+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Ahmed Khan","twitter_card":"summary_large_image","twitter_creator":"@ahmed0627","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Ahmed Khan","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/"},"author":{"name":"Ahmed Khan","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/39826783730ea673ddc3383fe3bcf855"},"headline":"Creating a complete blog (CRUD) using MySQL and PHP &#8211; Part 2","datePublished":"2016-08-22T09:15:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/"},"wordCount":630,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","keywords":["MySQL"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/","url":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/","name":"Creating a complete blog (CRUD) using MySQL and PHP - Part 2 - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-08-22T09:15:18+00:00","description":"In the previous 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","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/creating-complete-blog-crud-using-mysql-and-php-part2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"Creating a complete blog (CRUD) using MySQL and PHP &#8211; Part 2"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/39826783730ea673ddc3383fe3bcf855","name":"Ahmed Khan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9c0240c8a389a58b007699e24a2d7e937f6d150934678d723fb7037e3e5d5d56?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9c0240c8a389a58b007699e24a2d7e937f6d150934678d723fb7037e3e5d5d56?s=96&d=mm&r=g","caption":"Ahmed Khan"},"description":"Ahmed Khan is the PHP Community Manager at Cloudways, a hosting company that specializes in optimized PHP hosting services. He writes about PHP, MySQL and covers different tips and tricks related to PHP. He is currently active on Cloudways and other different blogs. When he is not writing about PHP, he likes watching The Flash, Game Of Thrones and is a die-hard fan of DC Comics.","sameAs":["https:\/\/www.facebook.com\/ahmedkhannn","https:\/\/x.com\/ahmed0627"],"url":"https:\/\/www.webcodegeeks.com\/author\/ahmed-khan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/14463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/174"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=14463"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/14463\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=14463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=14463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=14463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}