Add question timestamp and centralized initialization

This commit is contained in:
posweg 2020-05-21 11:57:27 +00:00
parent 30bc7ac4dc
commit b53cb2811c
3 changed files with 33 additions and 21 deletions

View File

@ -1,6 +1,30 @@
<?php <?php
session_start(); session_start();
$db = new SQLite3('ask.db');
$users_table = $db->query("CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY,
username TEXT,
shadow TEXT,
name TEXT,
bio TEXT
)");
$db->exec("CREATE TABLE IF NOT EXISTS questions(
id INTEGER PRIMARY KEY,
user INTEGER,
by INTEGER,
question TEXT,
date INTEGER,
answered INTEGER
);");
if(!$db->querySingle("SELECT EXISTS(SELECT * FROM users where id = 0);")){
echo "NOTICE: anonymous user created.";
$db->exec("INSERT INTO users(id, username, name) VALUES('0','anonymous','Anonymous');");
}
if($_SESSION["login"] === true){ if($_SESSION["login"] === true){
echo($_SESSION["uid"] . " - "); echo($_SESSION["uid"] . " - ");
echo("<a href='/logout'>logout</a>"); echo("<a href='/logout'>logout</a>");

View File

@ -2,14 +2,6 @@
session_start(); session_start();
$db = new SQLite3('ask.db'); $db = new SQLite3('ask.db');
$users_table = $db->query("CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY,
username TEXT,
shadow TEXT,
name TEXT,
bio TEXT
)");
$logErrorMsg = ""; $logErrorMsg = "";
$validUser = $_SESSION["login"] === true; $validUser = $_SESSION["login"] === true;
if(isset($_SESSION["login"])) $validUser = $_SESSION["login"] === true; if(isset($_SESSION["login"])) $validUser = $_SESSION["login"] === true;

View File

@ -7,18 +7,6 @@ if(!$user || !$user["id"]){
die(); die();
} }
$db->exec("CREATE TABLE IF NOT EXISTS questions(
id INTEGER PRIMARY KEY,
user INTEGER,
by INTEGER,
question TEXT
);");
if(!$db->querySingle("SELECT EXISTS(SELECT * FROM users where id = 0);")){
echo "NOTICE: anonymous user created.";
$db->exec("INSERT INTO users(id, username, name) VALUES('0','anonymous','Anonymous');");
}
if($validUser){ if($validUser){
header("Location: /"); die(); header("Location: /"); die();
} }
@ -33,7 +21,7 @@ if(isset($_POST["post-submit"])){
$question = htmlspecialchars($_POST["post-text"]); $question = htmlspecialchars($_POST["post-text"]);
// Insert user into DB // Insert user into DB
$db->exec("INSERT INTO questions(user,by,question) VALUES ('$u','$by','$question');"); $db->exec("INSERT INTO questions(user,by,question,answered,date) VALUES ('$u','$by','$question',0,". strtotime('now') .");");
unset($_POST["post-text"]); unset($_POST["post-text"]);
} }
} }
@ -58,10 +46,18 @@ if(isset($_POST["post-submit"])){
<?php <?php
$u_prep = $db->prepare("SELECT * FROM users WHERE id = :id"); $u_prep = $db->prepare("SELECT * FROM users WHERE id = :id");
$qs = $db->query("SELECT * FROM questions WHERE user = '" . $user["id"] . "' LIMIT 0, 10;"); $qs = $db->query("SELECT * FROM questions WHERE user = '" . $user["id"] . "' LIMIT 0, 10;");
$time = new DateTime("@0");
while($current = $qs->fetchArray(SQLITE3_ASSOC)){ while($current = $qs->fetchArray(SQLITE3_ASSOC)){
// Execute prepared statement
$u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER); $u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER);
$q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC); $q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC);
echo("<h3>". $q_user["name"] ."</h3>"); echo("<h3>". $q_user["name"] ."</h3>");
// Time
$time->setTimestamp($current["date"]);
echo($time->format("Y-m-d H:i:s"));
echo("<p>". $current["question"] ."<p>"); echo("<p>". $current["question"] ."<p>");
} }
?> ?>