Skip to content
Snippets Groups Projects
Commit 00bb8baf authored by junchh's avatar junchh
Browse files

add messages

parent 61b9a63a
Branches
No related merge requests found
......@@ -27,4 +27,14 @@ $users = [
];
$pdo = new PDO('mysql:host=' . HOST . ';dbname=' . DBNAME, USER, PASS);
try {
$pdo = new PDO('mysql:host=' . HOST . ';dbname=' . DBNAME, USER, PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}
?>
\ No newline at end of file
<?php
session_start();
require 'config.php';
if(!isset($_SESSION['user'])){
header('Location: login.php');
} else {
if(isset($_POST['submit']) && $_POST['content'] != ""){
$text = $_POST['content'];
try {
$pdo->exec("INSERT INTO `message`(`timestamp`, `username`, `message`, `id_parent`) VALUES (NOW(), '" . $_SESSION['user']['username'] . "', '" . $text . "', '0')");
} catch(Exception $e){
exit($e);
}
header ('Location: ' . $_SERVER['REQUEST_URI']);
exit();
}
}
?>
......@@ -14,6 +31,27 @@ if(!isset($_SESSION['user'])){
<title>Discussion Forum</title>
</head>
<body>
<h1>Welcome <?php echo $_SESSION['user']['username']; ?></h1>
<div class="container">
<form action="index.php" method="POST">
<textarea name="content" rows="4" cols="50"></textarea>
<input type="submit" value="post" name="submit" />
</form>
<div class="posts">
<?php
$query = $pdo->query("SELECT * FROM `message` WHERE `id_parent` = '0' ORDER BY `id` DESC");
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
echo '
<div class="message">
<h2>' . $row['username'] . '</h2>
<h3>' . $row['timestamp'] . '</h3>
<p>' . $row['message'] . '</p>
</div>
';
}
?>
</div>
</div>
</body>
</html>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment