//index.php
<?php
error_reporting(-1);
require_once ('config.php');
$queryResult = mysqli_query($link, "SELECT * FROM posts");
$headersNumber = $queryResult -> num_rows;
$posts = array();

for ($i=0; $i<$headersNumber; $i++) {
	$posts[] = mysqli_fetch_assoc($queryResult);
}
	
include_once('view.html');
?>


//post.php
<?php
error_reporting(-1);
if (isset($_POST['text'])) {
	$text = htmlspecialchars($_POST['text']);

	if (!empty($_POST['username'])) {
		$username = htmlspecialchars($_POST['username']);
	} else {
		$username = "Аноним";
	}
	
	unset($_POST['username']);
	unset($_POST['text']);
	require_once ('config.php');
	$queryResult = mysqli_query($link, "INSERT INTO `microboard`.`posts` (`name`, `message`) VALUES ('$username', '$text')");
}
header('Location: /index.php');
?>


//config.php
<?php
error_reporting(-1);
$db = array('host' => 'localhost',
			'user' => 'root',
			'pass' => '',
			'name' => 'microboard');
$link = mysqli_connect($db['host'], $db['user'], $db['pass'], $db['name']);
?>


//view.html

<!DOCTYPE html>
<html>

<head>
	<title>Microboard</title>
</head>

<body>
	<?php
	$num = count($posts);
	for($i=0; $i<$headersNumber; $i++) {
		echo "<p><b>№{$posts[$i]['id']}";
		echo "{$posts[$i]['name']}";
		echo "{$posts[$i]['posttime']}";
		echo "</b><br>";
		echo "{$posts[$i]['message']}";
		echo "</p><hr>"; 
	}
	?>
	
	<form method="post" action="/post.php">
		Name:
	<input type="text" name="username">
	<br>
	Комментарий:<br>
	<textarea type="text" name="text" rows="3" cols="64" required></textarea>
	<br>
	<input type="submit" value="Отправить пост">
	</form>

</body>

</html>

