I'm new to php and sql and I'm trying to build a new user system. I've tried two different methods and they both give me errors.
$_SERVER["REQUEST_METHOD"] == "POST" gives me an unidentified index 'Nom', 'Prenom', 'Role' and 'Mail' error
and isset($_POST['submit']) does not enter the if (doesn't print the test2)
So I'm guessing I have a problem with the form but I can't figure out what it is.
Edit : New problem : The sql query doesn't go through so the data isn't added into the database. I get this error message :Une petite erreur, veuillez réessayer plus tard ! aka the one from my last if else statement in the .php file
This is my html code (a form to collect data) :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="creation">
<h2>Créer un nouvel utilisateur</h2>
<p> Veuillez remplir les champs suivants</p>
<form action = "../Control/new_user.php" method = "post">
<label for = "Nom">
Nom <br>
</label>
<input type = "text" id = "Nom" name = "Nom" required></br>
<label for = "Prénom">
Prénom <br>
</label>
<input type = "text" id = "Prénom" name = "Prénom" required></br>
<label for = "Adressemail">
E-mail <br>
</label>
<input type = "email" id = "Adressemail" name = "Adressemail" required></br>
<label for = "Role">
Role <br>
</label>
<select name="role" id="role">
<option value="utilisateur">Utilisateur</option>
<option value="gestionnaire">Gestionnaire</option>
<option value="administrateur">Administrateur</option>
</select>
<br><br>
<input type = "submit" value = "Envoyer"/>
</form>
</div>
</body>
And this is my new_user.php file :
<?php
//Ouvrir la bdd
require_once "../Control/config.php";
//Création de variables
$Prenom = $Nom = $Mot_de_Passe = $Role = $Mail ="";
$Prenom_erreur = $Nom_Erreur = $Mdp_erreur = $Role_erreur = $Mail_erreur = "";
echo "test1";
//if(isset($_POST['submit'])){
if($_SERVER["REQUEST_METHOD"] == "POST"){
echo "test2";
// Validité Nom
if(empty(trim($_POST['Nom']))){
$Nom_Erreur = "Veuillez entrer un nom";
} else {
$Nom = trim($_POST['Nom']);}
// Validité Prénom
if(empty(trim($_POST["Prénom"]))){
$Prenom_erreur = "Veuillez entrer un prénom";
} else{
$Prenom = trim($_POST["Prénom"]);
}
//Verif mail
if(empty(trim($_POST["Adressemail"]))){
$Mail_erreur = "Veuillez entrer un mail";
} else{
$Mail = trim($_POST["Adressemail"]);
}
if(empty(trim($_POST["role"]))){
$Role_erreur = "Veuillez choisir un rôle";
} else{
if (trim($_POST["role"]) == 'Administrateur'){
$Role = 3;
} elseif(trim($_POST["role"]) == 'Gestionnaire'){
$Role = 2;
} else {
$Role = 1;
}
}
//Création d'un mdp aléatoire
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#$&*";
$length = strlen( $chars );
echo "Random string =";
for( $i = 0; $i < 6 ; $i++ ) {
$Mot_de_Passe= $chars[ rand( 0, $length - 1 ) ];
echo $Mot_de_Passe; }
// Verif erreurs
if(empty($Prenom_erreur) && empty($Mail_erreur) && empty($Mdp_erreur) && empty($Nom_Erreur) && empty($Role_erreur)){
$Mdp_code = password_hash($Mot_de_Passe, PASSWORD_DEFAULT);
$sql = "INSERT INTO utilisateurs (Nom, Prenom, Mail, Mot_de_Passe, Role) VALUES ($Nom,$Prenom,$Mail,$Mdp_code,$Role)";
if($db->query($sql) === TRUE){
//Si succès
echo "Utilisateur ajouté";
} else{
echo "Une petite erreur, veuillez réessayer plus tard ! ";
}
}
}
?>
I'd be extremely grateful of any help thanks !
$_POST['submit']
is never set because your submit input doesn't have aname="submit"
attribute – brombeer$_SERVER["REQUEST_METHOD"] == "POST"
is never hit because it should bemethod="post"
in your form, notmethode="post"
- so the form is being sent using GET – brombeer$Prénom
is not set, you named it$Prenom
– brombeer