<?php session_start(); $host = "127.0.0.1"; $port = "5432"; $user = "postgres"; $password = "postgres"; $db = "demo"; $conn_string = "host=" . $host . " port=5432 dbname=" . $db . " user=" . $user . " password=" . $password . ""; $con = pg_connect($conn_string); $username = $_SESSION['uname']; function unset_username() { unset($_SESSION['uname']); } if (isset($_GET['name'])) { unset_username(); } ?> <html> <head> <title>Login Page</title> <link rel="dns-prefetch" href="//fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="styles2.css"> <link rel="stylesheet" href="app.css"> <link rel="icon" type="image/png" href="img/favicon.png"/> </head> <body> <div id="app"> <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm"> <div class="container"> <a class="navbar-brand"> BattTho </a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav me-auto"> </ul> <ul class="navbar-nav ms-auto"> <?php echo ' <li class="nav-item dropdown"> <a id="navbarDropdown" class="nav-link" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre> user: ' . $username . ' </a> <li class="nav-item"> <a class="nav-link" href="login.php?name=true">Logout</a> </li> <script> function logout(){ <?php unset_username(); ?> } </script> '; ?> </li> </ul> </div> </div> </nav> </div> <br> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Make a question!</div> <div class="card-body"> <form method="GET" action=""> <div class="form-group"> <input type="text" name="ListQuestion" id="ListQuestion" class="form-control" placeholder="Enter question"> <button type="submit" name="submit" class="top-buffer">Submit Question</button> </div> </form> </div> </div> <br> <?php if (isset($_GET['submit'])) { $question = $_GET['ListQuestion']; $sql = "INSERT INTO questions (question, userquestion) VALUES ('$question', '$username')"; $rs = pg_query($con, $sql); header('Location: questions.php'); } if (isset($_GET['submit2'])) { $answer = $_GET['Answers']; $id = $_GET['currentId']; $sql = "INSERT INTO answers (answers, questionid, useranswer) VALUES('$answer', '$id', '$username')"; $rs = pg_query($con, $sql); header('Location: questions.php'); } $result = pg_query($con, 'SELECT question, "ID", userquestion FROM questions ORDER by "ID" DESC'); $result2 = pg_query($con, 'SELECT answers, questionid, useranswer FROM answers'); while ($row = pg_fetch_assoc($result)) { echo ' <div class="card"> <div class="card-header"> Question: ' . $row["question"] . ' <div>Question by: ' . $row["userquestion"] . '</div> </div> <div class="card-body"> <form method="GET" action="">'; pg_result_seek($result2, 0); while ($row2 = pg_fetch_assoc($result2)) { if ($row["ID"] == $row2["questionid"]) { echo '<p>' . $row2["useranswer"] . ': ' . $row2["answers"] . '</p>'; } } echo ' <input type="hidden" name="currentId" value="' . $row["ID"] . '" /> <div class="form-group"> <input type="text" name="Answers" class="form-control" placeholder="Enter answer"> <button type="submit" name="submit2" class="top-buffer">Submit Answer</button> </div> </form> </div> </div> <br> '; } ?> </div> </div> </div> </body>