diff --git a/index.php b/index.php index 8ac31e2..65b000f 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,30 @@ 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){ echo($_SESSION["uid"] . " - "); echo("logout"); diff --git a/login.php b/login.php index 6ce5243..e2023ba 100644 --- a/login.php +++ b/login.php @@ -2,14 +2,6 @@ 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 - )"); - $logErrorMsg = ""; $validUser = $_SESSION["login"] === true; if(isset($_SESSION["login"])) $validUser = $_SESSION["login"] === true; diff --git a/user.php b/user.php index 5dfd6d2..69d7273 100644 --- a/user.php +++ b/user.php @@ -7,18 +7,6 @@ if(!$user || !$user["id"]){ 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){ header("Location: /"); die(); } @@ -33,7 +21,7 @@ if(isset($_POST["post-submit"])){ $question = htmlspecialchars($_POST["post-text"]); // 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"]); } } @@ -58,10 +46,18 @@ if(isset($_POST["post-submit"])){ prepare("SELECT * FROM users WHERE id = :id"); $qs = $db->query("SELECT * FROM questions WHERE user = '" . $user["id"] . "' LIMIT 0, 10;"); + + $time = new DateTime("@0"); while($current = $qs->fetchArray(SQLITE3_ASSOC)){ + // Execute prepared statement $u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER); $q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC); + echo("

". $q_user["name"] ."

"); + + // Time + $time->setTimestamp($current["date"]); + echo($time->format("Y-m-d H:i:s")); echo("

". $current["question"] ."

"); } ?>