www/public/join/join.php

85 lines
2.4 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER['DOCUMENT_ROOT']);
session_start();
function create_member($name, $number, $email)
{
$root = realpath($_SERVER['DOCUMENT_ROOT']);
require("$root/config.php");
try{
$db = new PDO("pgsql:host=$host;dbname=$database",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
}catch (Exception $e){
die("Error : ".$e->getMessage());
}
// Check if table exitst
$req = $db->prepare('CREATE TABLE IF NOT EXISTS members (id SERIAL PRIMARY KEY, name varchar(125) NOT NULL UNIQUE, student_number varchar(8) UNIQUE, email varchar(125) UNIQUE)');
$req->execute();
if (strlen($name) > 125)
{
$_SESSION['error_msg'] .= "Le nom entré est trop long.\n";
}
if (strlen($email) > 125)
{
$_SESSION['error_msg'] .= "L'email entré est trop long.\n";
}
if (strlen($number) > 8)
{
$_SESSION['error_msg'] .= "Votre numéro d'étudiant semble incorrect (il comporte plus de 8 caractères.\n";
}
if ($_SESSION['error_msg'] == "")
{
try {
$req = $db->prepare('INSERT INTO members (name, student_number, email) VALUES (?, ?, ?)');
$req->execute(array($name, $number, $email));
} catch (Exception $e) {
$_SESSION['error_msg'] .= $e->getMessage();
}
}
}
$_SESSION['error_msg'] = "";
if (isset($_POST['submit']))
{
if (isset($_POST['name']) and $_POST['name'] != "")
{
$name = $_POST['name'];
$_SESSION['name'] = $name;
} else
{
$_SESSION['error_msg'] .= "Vous n'avez pas entré de nom.\n";
}
if (isset($_POST['number']))
{
$number = $_POST['number'];
} else {
$number = "";
}
if (isset($_POST['email']) and $_POST['email'] != "")
{
$email = $_POST['email'];
} else {
$_SESSION['error_msg'] .= "Vous n'avez pas entré d'adresse mail.\n";
}
} else
{
$_SESSION['error_msg'] .= "Attention, vous n'avez pas soumis le formulaire d'adhésion.\n";
}
if ($_SESSION['error_msg'] == "")
{
create_member($name, $number, $email);
}
if ($_SESSION['error_msg'] != "")
{
$_SESSION['error_msg'] .= "Veuillez réessayer.";
header('Location: /join');
} else {
header('Location: /');
}
?>