Skip to content

Instantly share code, notes, and snippets.

@guillaumebdx
Created October 14, 2021 14:27
Show Gist options
  • Save guillaumebdx/8a46dd13260cd1a9d9f20af66517297c to your computer and use it in GitHub Desktop.
Save guillaumebdx/8a46dd13260cd1a9d9f20af66517297c to your computer and use it in GitHub Desktop.
<?php
require 'secret.php';
$pdo = new PDO("mysql:host=" . SERVER . ";dbname=" . DATABASE . ";charset=utf8", USER, PASSWORD);
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = ucfirst(trim($_POST['name']));
$phone = trim($_POST['phone']);
if (empty($name)) {
$errors['name'] = 'Entrez un nom tchiiip';
}
if (strlen($name) > 255) {
$errors['name'] = 'Le nom est trop long';
}
if (empty($phone)) {
$errors['phone'] = 'Entrez un tel';
}
if (strlen($phone) > 255) {
$errors['phone'] = 'Le tel est trop long';
}
if (count($errors) === 0) {
$query = 'INSERT INTO user (name, phone) VALUES (:name, :phone)';
$statement = $pdo->prepare($query);
$statement->bindValue(':name', $name, PDO::PARAM_STR);
$statement->bindValue(':phone', $phone, PDO::PARAM_STR);
$statement->execute();
}
}
$query = 'SELECT * FROM user';
$statement = $pdo->query($query);
$users = $statement->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
<?php foreach ($errors as $error) : ?>
<?= $error ?>
<?php endforeach; ?>
</div>
<form action="" method="post">
<input type="text" placeholder="name" name="name" required maxlength="255">
<input type="tel" name="phone" placeholder="telephone" required maxlength="255">
<button>OK</button>
</form>
<div>
<ul>
<?php foreach ($users as $user) : ?>
<li>
Nom : <?= $user['name'] ?> - tel : <?= $user['phone'] ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<div>
Il y a <?= count($users) ?> personnes
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment