Edit is like add

That add form...

Here's a form for inserting data from last time, after the user had tried to make a record, but validation has failed.

Form

You know, that kinda looks like a form for editing data. There are fields. There's data in the fields. There's validation. Hmmm...

Hey y'all, what do we have to change to turn this...

MT form

... into a form for editing an existing comedian record?

Adela
Adela

There's be data in the fields. Like, you'd say, "I want to edit comedian Joe Bloggs, and you'd see the form, but with Joe's data in the fields, instead of MTness.

Good! What else?

Ray
Ray

Well, the labels would be different. Add would be Edit.

Right, that would change.

Ethan
Ethan

When the user clicked Save, the processing program would so something different. Change data, rather than making a new record.

Georgina
Georgina

Is that different SQL? We were using an INSERT, but that doesn't seem right.

Aye, that's true! There's an UPDATE statement. We'd have to use that.

Workflow

Here's the workflow diagram. Almost the same as before. I've added Sherlock Holmes where there are changes.

Workflow

​Workflow

The edit form program starts by loading the entity's existing data from the DB. The add page didn't load anything from the DB.

The form page still sends data to a save page, but it will be a different page from the insert one.

The processing page will run UPDATE, rather than INSERT.

Ethan
Ethan

Not many changes.

True.

The code

Here's the code for the edit form.

  1. <?php
  2. /**
  3.  * Edit comedian.
  4.  * This page validates, as well as showing the form.
  5.  */
  6. session_start();
  7. require_once 'library/useful-stuff.php';
  8. $errorMessage = '';
  9. // Make variables for field values.
  10. // Initialize them.
  11. $comedianName = null;
  12. $comedianComments = null;
  13. // Get the id of the comedian to edit.
  14. $comedianId = getParamFromGet('comedian_id');
  15. $errorMessage = checkComedianId($comedianId);
  16. if ($errorMessage == '') {
  17.     $comedianEntity = getComedianWithId($comedianId);
  18.     if (is_null($comedianEntity)) {
  19.         $errorMessage = "Sorry, problem loading comedian with id $comedianId.";
  20.     }
  21.     else {
  22.         // Put the field values into variables.
  23.         $comedianName = $comedianEntity['name'];
  24.         $comedianComments = $comedianEntity['comments'];
  25.     }
  26. }
  27. // Is there post data?
  28. if ($errorMessage == '' && $_POST) {
  29.     // User filled in data and sent it to this page.
  30.     // Grab the data sent.
  31.     // It will overwrite existing data in the field variables.
  32.     $comedianName = getParamFromPost('name');
  33.     $comedianComments = getParamFromPost('comments');
  34.     // Validate.
  35.     $errorMessage .= checkName($comedianName);
  36.     // Is everything OK?
  37.     if ($errorMessage == '') {
  38.         // No errors.
  39.         // Stash data for processing later.
  40.         $_SESSION['comedian_id'] = $comedianId;
  41.         $_SESSION['comedian_name'] = $comedianName;
  42.         $_SESSION['comedian_comments'] = $comedianComments;
  43.         // Off to processing.
  44.         header('Location: save-comedian.php');
  45.         exit();
  46.     }
  47. }
  48. ?><!doctype html>
  49. <html lang="en">
  50.     <head>
  51.         <?php
  52.         $pageTitle = 'Edit comedian';
  53.         require_once 'library/page-components/head.php';
  54.         ?>
  55.     </head>
  56.     <body>
  57.         <?php
  58.         require_once 'library/page-components/top.php';
  59.         print "<h1>$pageTitle</h1>\n";
  60.         if ($errorMessage != '') {
  61.             print "<p class='error-message'>$errorMessage</p>\n";
  62.         }
  63.         else {
  64.         ?>
  65.  
  66.             <form method="post">
  67.                 <p>
  68.                     <label>Name:
  69.                         <input type="text" name="name" value="<?php
  70.                         if (! is_null($comedianName)) {
  71.                             print $comedianName;
  72.                         }
  73.                         ?>">
  74.                     </label>
  75.                 </p>
  76.                 <p>
  77.                     <label>Comments:
  78.                         <textarea name="comments"><?php
  79.                             if (! is_null($comedianComments)) {
  80.                                 print $comedianComments;
  81.                             }
  82.                         ?></textarea>
  83.                     </label>
  84.                 </p>
  85.                 <p>
  86.                     <button type="submit">Save</button>
  87.                 </p>
  88.             </form>
  89.         <?php
  90.         }
  91.         require_once 'library/page-components/footer.php';
  92.         ?>
  93.     </body>
  94. </html>

Line 37 loads the existing data from the database. Lines 23 and 124 put the entity's field values into the variables $comedianName and $comedianComments.

Line 28 checks whether there is POST data. The first time the page is shown, there won't be. If there is post data, we put it into $comedianName and $comedianComments, replacing what was there from the record load.

If name passes validation, we stash data into the session for the save program (lines 40 to 42). Notice we stash the id as well. The add-comedian program didn't have a comedian id ready, since we were adding a new one. This program edits an existing record, so the id is there already.

The go-to-processing code (line 44) jumps to the different processing page. exit() is needed, since we don't want to put anything else into the output.

Processing

Here's the code for processing.

  1. <?php
  2. session_start();
  3. require_once 'library/useful-stuff.php';
  4. // Get the data to save.
  5. $comedianId = $_SESSION['comedian_id'];
  6. $comedianName = $_SESSION['comedian_name'];
  7. $comedianComments = $_SESSION['comedian_comments'];
  8. // Save the data to the DB.
  9. $sql = "
  10.     UPDATE comedians SET
  11.         name = :name,
  12.         comments = :comments
  13.     WHERE
  14.         comedian_id = :id;
  15. ";
  16. /** @var PDO $dbConnection */
  17. $stmnt = $dbConnection->prepare($sql);
  18. $stmnt->execute([
  19.     'id' => $comedianId,
  20.     'name' => $comedianName,
  21.     'comments' => $comedianComments
  22. ]);
  23. // Jump to the view page, passing new record id.
  24. header("Location: view-comedian.php?comedian_id=$comedianId");

Almost the same. We get the id for the record (line 5). We didn't have that for the add program.

An UPDATE instead of an INSERT (lines 9-15).

There used to be a line to get the last id that MySQL made, but we don't need that here.

The go-to-the-view-page code (line 24) is the same.

Operations links

Speaking of the view page, we could add some operation links to it:

Operations links

Cool! Here's the HTML and CSS.

  • print
  •     "<p class='operations-container'>
  •         <a href='edit-comedian-form.php?comedian_id={$comedianId}'>Edit</a>
  •         <a href='create-comedian-form.php'>Add new</a>
  •     </p>\n";
  •  
  • .operations-container a {
  •     margin-right: 2rem;
  • }

What's next?

Add and edit are similar. Let's see if we can combine the add and edit programs into one program.