<?php
    require_once("initPDO.php");

	if(isset($_POST['name']) && isset($_POST['email'])) {
        $add = $pdo->prepare("INSERT INTO `users` (`id`, `name`, `email`) VALUES (NULL, '" . $_POST['name'] . "', '" . $_POST['email'] . "')");
        $add->execute();
	}

    $request = $pdo->prepare("select * from users");
    $request->execute();
    $user = $request->fetch(PDO::FETCH_OBJ);

    $allUsers = '<table><tr><td>Id</td><td>Nom</td><td>Email</td></tr>';
    while(!empty($user)) {
        $allUsers .= '<tr><td>'.$user->id.'</td><td>'.$user->name.'</td><td>'.$user->email.'</td></tr>';
        $user = $request->fetch(PDO::FETCH_OBJ);
    }
    $allUsers .= "</table>";

    $pdo = null;

?>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<style>
		table {
			border-top: 1px solid black;
			border-bottom: 1px solid black;
		}

		td {
			text-align: center;
			padding-left: 2em;
			padding-right: 2em;
		}
		</style>
	</head>
	<body>
	<h1>Users</h1>
		<?php
			echo $allUsers;
		?>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<p>name : <input type="text" name="name" /></p>
			<p>email : <input type="text" name="email" /></p>
			<input type="submit" value="Add">
		</form>
	</body>
</html>
