1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| $data = [];
$errors = [];
$check_value_str = function(string $post_key, bool $required, bool $empty_allowed, string $control_name, string $db_field = '', int $min_length = null, int $max_length) use (&$data, &$errors)
{
if ($db_field === '') {
$db_field = $post_key;
}
if (isset($_POST[$post_key])) {
if ($_POST[$post_key] === '') {
if ($empty_allowed === false) {
$errors[] = $control_name.' invalide';
return;
}
} else {
if ($min_length !== null) {
if (mb_strlen($_POST[$post_key]) < $min_length) {
$errors[] = $control_name.' : valeur trop courte';
return;
}
}
if ($max_length !== null) {
if (mb_strlen($_POST[$post_key]) > $max_length) {
$errors[] = $control_name.' : valeur trop longue';
return;
}
}
$data[$db_field] = $_POST[$post_key];
}
} elseif ($required) {
$errors[] = $control_name.' obligatoire';
}
};
$check_value_int = function(string $post_key, bool $required, string $control_name, string $db_field = '', int $min = null, int $max = null) use (&$data, &$errors) {
if ($db_field === '') {
$db_field = $post_key;
}
if (isset($_POST[$post_key]) && ($_POST[$post_key] !== '')) {
if (ctype_digit($_POST[$post_key]) === false) {
$errors[] = $control_name.' : numérique attendu';
return;
} else {
if ($min !== null) {
if ($_POST[$post_key] < $min) {
$errors[] = $control_name.' : valeur supérieure attendue';
return;
}
}
if ($max !== null) {
if ($_POST[$post_key] > $max) {
$errors[] = $control_name.' : valeur inférieure attendue';
return;
}
}
$data[$db_field] = (int)$_POST[$post_key];
}
} elseif ($required) {
$errors[] = $control_name.' : valeur obligatoire';
}
};
// obligatoire
$check_value_str('nom', true, false, 'Nom', '', 1, 20);
$check_value_str('prenom', true, false, 'Prénom', '', 1, 20);
$check_value_str('pwd', true, false, 'Mot de passe', '', 1, 50);
$check_value_str('adresse_mail', true, false, 'Email', '', 1, 100);
$check_value_int('val', 'int', true, 'Valeur', 0, 9);
// facultatif
$check_value_int('num', 'int', false, 'Numéro', 0, 99999999);
$check_value_str('rue', false, true, 'Rue', '', 0, 30);
$check_value_int('code', 'int', false, 'Code postal', 0, 99999999);
$check_value_str('localite', false, true, 'Localité', '', 0, 50);
$check_value_str('gsm', false, true, 'GSM');
$check_value_str('tel', false, true, 'Téléphone');
$check_value_int('affil', 'int', false, 'Affiliation', 0, 999999999);
if ( ! empty($errors)) {
echo implode('<br>', $errors);
exit;
}
if (empty(filter_var($data['adresse_mail'], FILTER_VALIDATE_EMAIL))) {
echo 'Adresse email invalide';
exit;
}
// sql
$values = [];
foreach ($data as $db_fields => $v) {
$values[] = is_int($v) ? $v : '?';
}
$sql = 'INSERT INTO yves2 ('.implode(', ', array_keys($data)).') VALUES ('.implode(', ', $values).')';
$stmt = $con->prepare($sql);
$exec = $stmt->execute($data);
if ($exec) {
// réussite
} else {
// échec
} |