<html>
<body>
<link rel="stylesheet" href="style.css">
<?php include 'header.php'; ?>
<?php
require_once 'database.php';
require_once 'history.php';
require_once 'usercheck.php';
require_once 'errors.php';
// connect to the db
ttdb_connect($connection);
// Check admin privilege
if (ttus_userIsAdmin($connection) != "t") {
tter_errorWithBackButton("You are not authorized to perform this operation", "Access level error");
exit;
}
ttdb_beginTransaction($connection);
switch ($__operation) {
case "add":
// get field names
$query = "select * from $__table";
$res = ttdb_execQuery($connection, $query);
// replace the values of the 1st reg. with the added ones in the array
$query = "INSERT INTO $__table (";
$fieldCount = ttdb_getNumFields($res);
for ($a = 0; $a < $fieldCount; $a++) {
$key = ttdb_getFieldName($res, $a);
$query .= " $key";
if ($a != $fieldCount - 1) $query .= ", ";
}
$query .= ") VALUES (";
for ($a = 0; $a < ttdb_getNumFields($res); $a++) {
$key = ttdb_getFieldName($res, $a);
$type = ttdb_getFieldType($res, $a);
$value = $HTTP_POST_VARS[$key];
if ($value == "")
$query .= "null";
else {
if ($type == "int4")
$query .= " $value";
else
$query .= " '$value'";
}
if ($a != $fieldCount - 1) $query .= ", ";
}
$query .= ")";
$caption = "Register added";
break;
case "delete":
// build delete query
$query = "DELETE FROM $__table WHERE $__keyfield = $__keyvalue";
$caption = "Register deleted";
break;
default:
// edit operation
$query = "select * from $__table";
$res = ttdb_execQuery($connection, $query);
$fields = ttdb_getArray($res); // take the first register
// replace the values of the 1st reg. with the added ones in the array
$query = "UPDATE $__table SET ";
$a = 0;
foreach ($fields as $key => $value) {
$type = ttdb_getFieldType($res, $a);
if ($HTTP_POST_VARS[$key] == "")
$query .= " $key = null";
else {
if ($type == "int4")
$query .= " $key = {$HTTP_POST_VARS[$key]}";
else
$query .= " $key = '{$HTTP_POST_VARS[$key]}'";
}
if (++$a != count($fields)) $query .= ", ";
}
$query .= " WHERE $__keyfield = $__keyvalue";
$caption = "Register modified";
}
// issue query and check result
//echo "$query<br>";
//ttdb_rollbackTransaction($connection);
//exit;
if (ttdb_execQuery($connection, $query) == 0) {
tter_errorWithBackButton("'$query' returned error.");
ttdb_rollbackTransaction($connection);
exit;
}
ttdb_commitTransaction($connection);
ttdb_close($connection);
?>
<h1><?php echo $caption?></h1>
<p>Click <a href="admintable.php?table=<?php echo $__table?>&keyfield=<?echo $__keyfield?>">here</a> to go back to the table contents.
</p>
<p> <?php include 'footer.php'; ?> </p>
</body>
</html>