-1

PHP file is:

<?php

require_once("connexion.php");

$nom = $prenom = $adresse = $age = "";

if  ($_SERVER["REQUEST_METHOD"] == "GET") {

$nom = $_GET["nom"];
$prenom = $_GET["prenom"];
$adresse = $_GET["adresse"];
$age = $_GET["age"];

}

$sql = "INSERT INTO etudiant (nom, prenom, adresse, age) VALUES (?, ?, ?, ?)";

$stmt = mysqli_prepare($connexion, $sql);

mysqli_stmt_bind_param($stmt, "ssss", $param_nom, $param_prenom, $param_adresse, $param_age);

$param_nom = $nom;
$param_prenom = $prenom;
$param_adresse = $adresse;
$param_age = $age;

if (!empty($param_nom) && !empty($param_prenom) && !empty($param_adresse) && !empty($param_age)) {

$stmt -> execute();

}


mysqli_close($connexion);

?>

My HTML :

<html>
<style>

</style>
<body>

<h2> Ajouter un nouveau étudiant</h2>

<p>Merci de remplir tous les champs afin d'ajouter un nouveau étudiant</p>

<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <label for="">Nom :</label>
    <input type="text" name="nom" placeholder=" Nom *" value="<?php echo $nom; ?>">
    </br></br>
    <label for="">Prénom :</label>
    <input type="text" name="prenom" placeholder=" Prénom *" value="<?php echo $prenom; ?>">
    </br></br>
    <label for="">Adresse :</label>
    <input type="text" name="adresse" placeholder=" Adresse *" value="<?php echo $adresse; ?>">
    </br></br>
    <label for="">Age :</label>
    <input type="numeric" name="age" placeholder=" Age *" value="<?php echo $age; ?>">
    </br></br>
    <input type="submit" name="envoyer" value="Submit"  onclick="self.location.href='affichage_etudiant.php'">
    <input type="reset" name="reset" value="Annuler">
</form>

</body>
</html>

I'm working on php forms using the GET method an I got this error, please help !

When I complete my form and I send my form, result is:

Notice: Undefined index in line 9, 10, 11, 12.

I used the GET method because they asked me to do with like that, Grr.

Siwams
  • 19
  • 3
  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – B001ᛦ May 10 '21 at 11:03
  • 2
    If the page is loaded without submitting the FORM, it is an GET request as well, but without any data... you'd have to check not only the HTTP verb but also, the expected variables are present! – Honk der Hase May 10 '21 at 11:08
  • 1
    Also, is your intention really to insert a row full of blank values when the form isn't submitted? It seems to me that you'd want to wrap the entire code in a condition that checks if the data is set. – El_Vanja May 10 '21 at 11:11

1 Answers1

-1

Change your GET method like below:

if  ($_SERVER["REQUEST_METHOD"] == "GET") {

  $nom = isset($_GET["nom"]) ? $_GET["nom"] : '';
  $prenom = isset($_GET["prenom"]) ? $_GET["prenom"] : '';
  $adresse = isset($_GET["adresse"]) ? $_GET["adresse"] : '';
  $age = isset($_GET["age"]) ? $_GET["age"] : '';

}
B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
Omar
  • 527
  • 5
  • 21