We are now going to build the Blog Editor, a page that loads an article, specified by the _id
, from the database into an HTML form. The user changes the title and/or the content of the article, hits save, and the article gets updated with the modified content.
1. Create a new PHP file named
edit.php
. Add the following code to the file:<?php $action = (!empty($_POST['btn_submit']) && ($_POST['btn_submit'] === 'Save')) ? 'save_article' : 'show_form'; $id = $_REQUEST['id']; try { $mongodb = new Mongo(); $articleCollection = $mongodb->myblogsite->articles; } catch (MongoConnectionException $e) { die('Failed to connect to MongoDB '.$e->getMessage()); } switch($action){ case 'save_article': $article = array(); $article['title'] = $_POST['title']; $article['content'] = $_POST['content']; $article[saved_at] = new MongoDate(); $articleCollection->update(array('_id' => new MongoId($id)), $article); break; case 'show_form': default...