Home > Articles > Programming > PHP

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Updating Data

What if the event already exists, and we just want to edit it? Our last task will be to update the event information form so that it can also update existing database information.

In a web application, editing is normally handled by pre-filling the form with the existing information. To do that, we'll need to supply saveevent.php with the eventid. The simplest way to accomplish this is to add a link to the bottom of showevent.php:

...
   printf ( "<p>%s</p>", $event["eventDesc"] );
   printf ( "<a href='saveevent.php?editid=%s'>Edit this event</a>", 
                               $eventid);

}
...

From there, we can create variables for the existing information and add them to the form, as shown in Listing 10.

Listing 10—Adding Existing Information to the Form (saveevent.php)

<html>
<head><title>Event Information Form</title></head>
<body>
  <h2>Event Information Form</h2>
<?php

import_request_variables('p', '');
import_request_variables('g', 'g_');

if (isset($month)) {

  // Connect to database.
  $connection = mysql_connect ( "localhost", "myusername", "mypassword" ) 
     || die ( "Error connecting to database!" );

  $insert_statement =
      "insert into events ".
      "(eventMonth, eventDay, eventYear, eventTitle, eventDesc)".
      " values ".
      "($month, $day, $year, '$title', '$description')";

  if (mysql_db_query ( "mysql", $insert_statement)) {
    echo("<p>Event '$title' added.</p>");
  } else {
    die ("Cannot add event.");
  }
} elseif (isset($g_editid)) {

  // Connect to database.
  $connection = mysql_connect ("localhost", "myusername", "mypassword")
     || die ( "Error connecting to database!" );

  $select_statement = 
     "SELECT eventTitle, eventDesc, eventMonth, eventDay, eventYear ".
          "from events where eventid=$g_editid";

  $results = mysql_db_query ( "mysql", $select_statement );

  while ($event = mysql_fetch_array ( $results )) {

    $d_month = $event["eventMonth"];
    $d_day = $event["eventDay"];
    $d_year = $event["eventYear"];
    $d_title = $event["eventTitle"];
    $d_description = $event["eventDesc"];
  }
}
?>
  <form action="saveevent.php" method="post">
    <input type="hidden" name="oldid" value="<?= $g_editid ?>" />
    Month: 
     <input type="text" name="month" value="<?= $d_month ?>" /><br />
    Day: 
     <input type="text" name="day" value="<?= $d_day ?>" /><br />
    Year: 
     <input type="text" name="year" value="<?= $d_year ?>" /><br />
    Title: 
     <input type="text" name="title" value="<?= $d_title ?>" /><br />
    Description: <textarea 
       name="description" cols="60" rows="4"><?= $d_description ?>
           </textarea><br />
    <input type="submit" value="Save Event" />
  </form>
</body>
</html>

Here we're once again using the technique of checking whether a form field was set. Because this particular form field, editid, would be specified as part of the URL, we need to specifically import the GET variables. To avoid any potential confusion, we'll import them with a g_ prefix.

Once we've determined that the form isn't being submitted, we'll check to see if an event to edit has been specified. If so, we'll select the information from the database and then store it in variables. The variables themselves are added to the form, so when it's displayed, it's already populated, as shown in Figure 9. The form itself uses the "abbreviated method" for outputting text.

Figure 9Figure 9 The form can be pre-populated with event information.

When the form is actually submitted, we can check whether the oldid variable has been set; if it has, we have to update information rather than creating it, as shown in Listing 11.

Listing 11—Updating Information in the Database (saveevent.php)

<html>
<head><title>Event Information Form</title></head>
<body>
  <h2>Event Information Form</h2>
<?php

import_request_variables('p', '');
import_request_variables('g', 'g_');

if (isset($month)) {

  // Connect to database.
  $connection = mysql_connect ("localhost", "myusername", "mypassword")
     || die ( "Error connecting to database!" );

  if ($oldid != "") {
    //Edit existing event
    $update_statement =
        "update events ".
        "set eventMonth = $month, eventDay = $day, ".
        " eventYear = $year, eventTitle = '$title',". 
        " eventDesc = '$description' ".
        "where eventid = $oldid";

    if (mysql_db_query ( "mysql", $update_statement)) {
      echo("<p>Event '$title' updated.</p>");
    } else {
      die ("Cannot udpate event");
    }

  } else {
    //Create new event
    $insert_statement =
        "insert into events ".
        "(eventMonth, eventDay, eventYear, eventTitle, eventDesc)".
        " values ".
        "($month, $day, $year, '$title', '$description')";

    if (mysql_db_query ( "mysql", $insert_statement)) {
      echo("<p>Event '$title' added.</p>");
    } else {
      die ("Cannot add event.");
    }
  }
} elseif (isset($g_editid)) {

  // Connect to database.
  $connection = mysql_connect ("localhost", "myusername", "mypassword")
     || die ( "Error connecting to database!" );
...

Here the basic idea is the same, but we can't just check whether $oldid has been set—it will always be set if the form is being submitted. Instead, we check to see whether it's empty. If it's not empty, updating the database is a simple matter of executing an update statement rather than an insert or select statement. The rest of the page behaves just as before.

  • + Share This
  • 🔖 Save To Your Account