mirror of
https://github.com/fsr/eseeva
synced 2024-11-15 00:43:12 +01:00
fix errors in database interaction
This commit is contained in:
parent
fe8a426334
commit
576d90f074
|
@ -37,7 +37,7 @@
|
|||
session_start();
|
||||
$formState = STATE_ACTION_NEWCODE;
|
||||
$keyCode = "";
|
||||
$keyData = ReadKeyFile(KEYFILE);
|
||||
$keyData = ReadKeys(KEYFILE);
|
||||
|
||||
// if the variable is set, the form has been posted to itself
|
||||
// if the submission ids of the post and the form don't match, the
|
||||
|
@ -55,8 +55,9 @@
|
|||
$keyCode = $_POST["keyCode"];
|
||||
//$keyData = ReadKeyFile(KEYFILE);
|
||||
// if no action was performed on the entered key, simply display its state
|
||||
if (!isset($_POST["action"]))
|
||||
$formState = KeyStateToFormState(GetKeyState($keyData, $_POST["keyCode"]));
|
||||
if (!isset($_POST["action"])){
|
||||
$formState = KeyStateToFormState(GetKeyState(KEYFILE, $_POST["keyCode"]));
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise set the state of the form to the action that should be performed
|
||||
|
@ -65,8 +66,8 @@
|
|||
// on the key and if the action was successful save the key file
|
||||
if ($formState != STATE_ACTION_NEWCODE)
|
||||
{
|
||||
if (SetKeyState($keyData, $keyCode, FormStateToKeyState($formState)))
|
||||
WriteKeyFile(KEYFILE, $keyData);
|
||||
if (SetKeyState(KEYFILE, $keyCode, FormStateToKeyState($formState)))
|
||||
SetKeyState(KEYFILE, $keyCode, FormStateToKeyState($formState));
|
||||
else
|
||||
$formState($STATE_ACTION_FAILED);
|
||||
$_POST["action"] = STATE_ACTION_NEWCODE;
|
||||
|
@ -205,11 +206,11 @@
|
|||
echo " <div class=\"col-6\">\n";
|
||||
echo " <select class=\"form-control\" id=\"keyCode\" name=\"keyCode\" required>\n";
|
||||
foreach ($keyData as $key => $value) {
|
||||
echo " <option value=\"". $value[0] ."\" ";
|
||||
if ($keyCode != "" && $keyCode == $value[0]) {
|
||||
echo " <option value=\"". $value['KeyId'] ."\" ";
|
||||
if ($keyCode != "" && $keyCode == $value['KeyId']) {
|
||||
echo "selected=\"selected\"";
|
||||
}
|
||||
echo ">".$value[0]."</option>\n";
|
||||
echo ">".$value['KeyId']."</option>\n";
|
||||
}
|
||||
echo " </select>\n";
|
||||
//echo " <input class=\"form-control\" type=\"text\" id=\"keyCode\" name=\"keyCode\" value=\"" . $keyCode . "\" required";
|
||||
|
|
|
@ -31,8 +31,9 @@
|
|||
$i = 0;
|
||||
while(isset($_POST["keyIndex" . $i]))
|
||||
{
|
||||
if (isset($_POST["keyDelete" . $i]))
|
||||
DeleteKey($keyFile, $_POST["keyCode" . $i]);
|
||||
if (isset($_POST["keyDelete" . $i])){
|
||||
$delete = DeleteKey($keyFile, $_POST["keyCode" . $i]);
|
||||
}
|
||||
else
|
||||
SetKeyState($keyFile, $_POST["keyCode" . $i], $_POST["keyState" . $i]);
|
||||
$i++;
|
||||
|
@ -52,7 +53,7 @@
|
|||
{
|
||||
$amount = $_POST["keyAmount"];
|
||||
$keyFile = $_POST["keyFile"];
|
||||
CreateKeys($amount, $keyFile);
|
||||
AppendKeys($amount, $keyFile);
|
||||
$keyData = ReadKeys($keyFile);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -55,6 +55,36 @@
|
|||
* @param string $fileName The name of the key database that should be created.
|
||||
*/
|
||||
function CreateKeys($keyAmount, $fileName)
|
||||
{
|
||||
$handle = new SQLite3($fileName);
|
||||
if (!$handle)
|
||||
return false;
|
||||
$handle->exec("DROP TABLE answers;");
|
||||
$handle->exec("CREATE TABLE IF NOT EXISTS 'answers'(KeyId text primary key,Status text,Student int,Answer);");
|
||||
$keys = GenerateKeys($keyAmount);
|
||||
$count = count($keys);
|
||||
|
||||
for ($i = 0; $i < $count; $i++)
|
||||
{
|
||||
$stmt = $handle->prepare("INSERT INTO answers (KeyId, Status) VALUES (:key,:status);");
|
||||
if ($stmt)
|
||||
{
|
||||
$stmt->bindValue(':key', $keys[$i], SQLITE3_TEXT);
|
||||
$stmt->bindValue(':status', KEYSTATE_UNISSUED, SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
}
|
||||
else
|
||||
{
|
||||
$handle->close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$handle->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
function AppendKeys($keyAmount, $fileName)
|
||||
{
|
||||
$handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE);
|
||||
if (!$handle)
|
||||
|
@ -186,7 +216,7 @@
|
|||
return false;
|
||||
|
||||
//secure deleting by checking if key has been uesed already
|
||||
$stmt = $handle->prepare("DELETE FROM answers WHERE KeyId=:keyid AND Answer!=NULL;");
|
||||
$stmt = $handle->prepare("DELETE FROM answers WHERE KeyId=:keyid;");
|
||||
if ($stmt)
|
||||
{
|
||||
$stmt->bindValue(':keyid', $key, SQLITE3_TEXT);
|
||||
|
|
Loading…
Reference in a new issue