O'Reilly logo

PHP and MongoDB Web Development by Rubayeet Islam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - building the Blog Editor

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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required