<?php
  // This is a sample form for editing a record and it's uploads

  require_once "../lib/viewer_functions.php";
  $tableName       = 'every_field_multi';
  $recordNum       = @$_REQUEST['num'] ? intval($_REQUEST['num']) : getLastNumberInUrl();
  $preSaveTempId   = null; // you must set either $recordNum or $preSaveTempId to null
  $errorsAndAlerts = '';

  ### load record
  $query  = mysql_escapef("SELECT * FROM {$TABLE_PREFIX}$tableName WHERE num = ? LIMIT 1", $recordNum);
  $record = mysql_get_query($query);
  if (!$recordNum) { die("No record number specified in url!"); }
  if (!$record)    { header("HTTP/1.0 404 Not Found"); die("Record #$recordNum not found!"); }


  ### pre-populate form with record values
  // ... this is an easy way to get the record value, or what the user last entered for
  // ... redisplaying the form with their last entered value and any errors
  foreach ($record as $name => $value) {
    if (array_key_exists($name, $_REQUEST)) { continue; }
    $_REQUEST[$name] = $value;
  }


  ### Update record
  if (@$_REQUEST['submitForm']) {

    // error checking
    $errorsAndAlerts = '';
    if (!@$_REQUEST['title'])    { $errorsAndAlerts .= "Please specify title!<br/>\n"; }

    // update record
    if (!@$errorsAndAlerts) {
      $colsToValues = [
        'updatedDate='     => 'NOW()',
        'updatedByUserNum' => @$CURRENT_USER['num'],
        'title'            => @$_REQUEST['title'],
      ];
      mysql_update($tableName, $recordNum, null, $colsToValues);

      // display thanks message and clear form
      $errorsAndAlerts = "Thanks, we've updated your record!";
    }
  }

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
  body, td { font-family: arial }
</style>
</head>
<body>


<h1>Update Record</h1>

<?php if (@$errorsAndAlerts): ?>
  <div style="color: #C00; font-weight: bold; font-size: 14px;"><?php echo $errorsAndAlerts; ?></div>
<?php endif ?>

<hr/>

<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input type="hidden" name="submitForm" value="1" />
<input type="hidden" name="num" value="<?php echo $recordNum ?>" />
<input type="hidden" name="preSaveTempId" value="<?php echo htmlencode($preSaveTempId) ?>" />

<table border="0" cellspacing="0" cellpadding="2">
 <tr>
  <td valign="top">Record Num</td>
  <td><?php echo $record['num'] ?></td>
</tr>
 <tr>
  <td valign="top">Title</td>
  <td><input class="text-input medium-input" type="text" name="title" value="<?php echo htmlencode(@$_REQUEST['title']) ?>" size="30" /></td>
</tr>
 <tr>
  <td valign="top">Uploads</td>
  <td>

    <?php /* TODO: Add security check in uploadForm3_iframe.php to limit access to only allowed uploads */ ?>
    <iframe src='uploadForm3_iframe.php?table=<?php echo $tableName ?>&amp;field=upload&amp;num=<?php echo $recordNum ?>&amp;preSaveTempId=<?php echo htmlencode($preSaveTempId) ?>'
            height='100' width='600' frameborder='0' scrolling='no'>
    </iframe><br/>

  </td>
</tr>
</table><br/>
<hr/>
<input class="btn btn-primary" type="submit" name="submitForm" value="Update Record &gt;&gt;" />

</form>

</body>
</html>
