} else {
        list($success, $m) = deleteRecord($db, $table, $deleteId);
        if (strlen($msg > 0)) {
            $msg .= " / " . $m;
        } else {
            $msg = $m;
        }
        // We do a redirect so that the 'deleteId' won't remain in the url.
        // *** RFD: There's got to be a better way!
        header("Location: " . $_SERVER["PHP_SELF"] . "?table=" . $table . "&msg=" . urlencode($msg));
        exit;
    }
}
if (isset($_REQUEST["updateId"])) {
    $d[$table]['id'] = $_REQUEST["updateId"];
} else {
    $d[$table]['id'] = 0;
}
// Here we actually start writing the HTML.
writeHeader("Edit {$table}", "secure", $msg);
wizardHeader(1);
if ($d[$table]['id'] != 0) {
    echo "<h1>Update entry in table '{$table}':</h1>\n";
} else {
    echo "<p><font size=+1><b>Add entry to table '{$table}':</b></font></p>\n";
}
$selfUrl = "https://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
// The following function requires the non-trivial code above to process the
// data, so I'm not sure that it makes sense to keep it in a separate function.
echo buildFormFromTable($db, $table, $prevTable, $selfUrl, $d, $extras, $defaultDataId);
writeFooter('basic');
}
if (isset($_REQUEST["sortBy"])) {
    $sortBy = $_REQUEST["sortBy"];
} else {
    $sortBy = "";
}
if (isset($_REQUEST["sortDir"])) {
    $sortDir = $_REQUEST["sortDir"];
} else {
    $sortDir = "ASC";
}
$db = login();
$selfUrl = "https://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
$msg = "";
writeHeader("Building session- build new session", "secure", $msg);
wizardHeader(2);
//echo "<p><font size=+1><a href=\"selectExistingSession.php?table=sessions&returnURL=".urlencode($selfUrl)."&returnIdName=sessionId\">Select existing session</a>\n";
//echo "&nbsp;&nbsp; or add a new session:\n</font></p>";
$idStr = '<a href="buildSession3.php?studyId=' . $studyId . '&sessionId=<ID>"><ID></a>';
$sortbyStr = '<a href="' . selfURL() . '?table=' . $table . '&studyId=' . $studyId . '&sortDir=<DIR2>&sortBy=<ID>"><ID><DIR></a>';
if ($sortBy == "") {
    // Default sort
    $sortBy = 'id';
}
$displaySummary = 1;
echo "<p><font size=+1><b>Select from the following existing sessions:</b></font></p>";
//tableText = displayTable($db, $table, $idStr, "WHERE primaryStudyID=".$studyId, 0, $displaySummary, "", $studyId, $subjectId, $sessionId, "", "", "", "");
$tableText = displayTable($db, $table, $idStr, "WHERE primaryStudyID=" . $studyId, 0, $displaySummary, "", $studyId, 0, $sessionId, $sortbyStr, $sortBy, $sortDir, "");
if ($tableText != "") {
    echo $tableText;
} else {
    }
}
if (isset($_REQUEST["updateId"])) {
    $d[$table]['id'] = $_REQUEST["updateId"];
} else {
    $d[$table]['id'] = 0;
}
if (isset($_REQUEST["scanId"])) {
    $scanId = $_REQUEST["scanId"];
    $defaultDataId = $scanId;
}
if ($scanIdSet) {
    header("Location: buildSessionFinish.php?studyId=" . $studyId . "&sessionId=" . $sessionId . "&scanId=" . $scanId);
}
writeHeader("Building session- build new scan", "secure", $msg);
wizardHeader(3);
$q = "SELECT sessionCode,primaryStudyID FROM sessions WHERE id=" . $sessionId;
if (!($res = mysql_query($q, $db))) {
    print "\n<p>mrData ERROR: " . mysql_error($db);
    exit;
}
$array0 = mysql_fetch_row($res);
$sessionTitle = $array0[0];
$studyId = $array0[1];
//echo "resstr[0]=".$resstr[0];
//echo "resstr[1]=".$resstr[1];
$q = "SELECT title FROM studies WHERE id=" . $studyId;
if (!($res = mysql_query($q, $db))) {
    print "\n<p>mrData ERROR: " . mysql_error($db);
    exit;
}