Skip to content

Instantly share code, notes, and snippets.

@Orianne0605
Created October 21, 2019 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orianne0605/be67c9078a45eec200ef99379ffe493b to your computer and use it in GitHub Desktop.
Save Orianne0605/be67c9078a45eec200ef99379ffe493b to your computer and use it in GitHub Desktop.
PDO
<?php
require_once 'connec.php';
try {
$pdo = new \PDO(DSN, USER, PASS);
} catch (PDOException $e) {
echo 'Connexion échouée : ' . $e->getMessage();
}
$requete = "SELECT * FROM friend";
$résultat = $pdo->query($requete);
$cherche = $résultat->fetchAll(PDO::FETCH_ASSOC);
var_dump($cherche);
$errors = [
"errorLastname" => "",
"errorFirstname" => "",
];
$validation=false;
if($_SERVER["REQUEST_METHOD"] == "POST") {
$lastname = $_POST[('lastname')];
$firstname = $_POST[('firstname')];
if(empty($lastname)){
$errors["errorLastname"]= "<small> Vous n'avez pas renseigné votre nom</small>";
} elseif (!preg_match("/^[a-zA-Z ]*$/", $lastname)) {
$errors["errorLastName"] = "<small>Only letters and white space allowed</small>";
}
if(empty($firstname)){
$errors["errorFirstname"]= "<small> Vous n'avez pas renseigné votre prénom</small>";
}elseif (!preg_match("/^[a-zA-Z ]*$/", $firstname)) {
$errors["errorFirstname"] = "<small>Entrez un nom valide</small>";
}
if ($errors['errorLastname']== "" && $errors['errorFirstname']==""){
$validation = true;
$insertForm = 'INSERT INTO friend (firstname, lastname) VALUES (:firstname, :lastname)';
$pushMe = $pdo->prepare($insertForm);
$pushMe->execute([
':firstname' => $firstname,
':lastname' => $lastname]);
}
header('Location: index.php');
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Friends</title>
</head>
<body>
<p>Friends Characters</p>
<form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]);?>" method= "POST">
<div>
<label for="lastname">Nom :</label>
<input type="text" id="lastname" name="lastname">
<?= $errors["errorLastname"] ?>
</div>
<div>
<label for="firstname">Prénom :</label>
<input type="text" id="firstname" name="firstname">
<?= $errors["errorFirstname"] ?>
</div>
<div class="button">
<button type="submit">Envoyer votre message</button>
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment