function postComment($comment) { session_start(); global $con; try { $sql = "INSERT INTO `sports_comments` (`ID`, `USER`, `COMMENT`) VALUES (NULL, :user, :comment)"; $sql = $con->prepare($sql); $sql->bindParam(':user', $_SESSION['username']); $sql->bindParam(':comment', $comment); $sql->execute(); } catch (PDOException $e) { echo $e; return; } buildCommentSection(getComments()); }
function getComments() { global $con; try { $sql = "SELECT * FROM `comments` WHERE `title` = :title"; $sql = $con->prepare($sql); $sql->bindParam(':title', $_SESSION['title']); $sql->execute(); buildCommentSection($sql->fetchAll()); } catch (PDOException $e) { echo $e; } }
<div class="wrap"> <link rel="stylesheet" type="text/css" href="/phase5/css/sports.css"> <div class="dropdown"> <script src="/phase5/js/sports.js"></script> </div> <div class="row"> <div class="col-md-6" id="sportsBox"> </div> <div class="col-xs-6 col-md-6"> <div class="panel panel-primary"> <div class="panel-heading"> <h4>SportsChat</h4> </div> <div class="panel-body" id="comments" style = "overflow: scroll;height: 500px; overflow-x: hidden;";> <?php buildCommentSection(getComments()); ?> </div> <div class="panel-footer"> <?php if (isset($_SESSION['loggedin'])) { ?> <div class="form-group"> <textarea id="comment" name="comment" cols="75" rows="3" maxlength="150"></textarea> <input class="btn btn-lg pull-right" id="post" type="submit" value="Post Comment"> </div> <?php } else { ?> <div class="center-block"> <p> You must be <a href="login.php">logged in</a> to post comments. </p>