Hi everyone here,
I'm trying to show information about the material according to the year of meeting(is a meeting when they decide to buy a new material)
so I use a drop-down list and the information will show according to the year chosen by the user, but it shows the error as is mentioned in the title.
this the code.
Thanks in advance.

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<div class="container">
<form method="POST" action="" name="form">
   <div class="row">
    <div class="col-25">
      <label for="year">annee marche</label>
    </div>
    <div class="col-75">
    <?php
require("connexion.php");
$sql = "SELECT DISTINCT(annee) FROM  marche";
 $result = mysqli_query($conn, $sql);
 print '<select name="year">';
 if (mysqli_num_rows($result) > 0) {

  while($row = mysqli_fetch_assoc($result)) {

print '<option value="'.$row ['annee'].'" >'.$row ['annee'].'</option>';
}}
print '</select>';
?> 
  </div></div>

</form>  
</div>
<div >
<table class="table">
  <thead>
      <tr>
    <th>material</th>
    <th>serial number</th>
    <th> inventory number </th>
    <th>type</th>
    <th>state</th>
    <th>number_meeting</th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
  </thead>
  <tbody>
  <?php  require("connexion.php");
  //  if ($_SERVER["REQUEST_METHOD"] == "POST"){
          $an=mysqli_real_escape_string($conn,$_POST["year"]);
            $sql="SELECT id_mat, libelle_mat, num_serie, num_inv,etat,libelle_type,num_mar FROM materiel,type,marche WHERE type.id_type_mat=materiel.id_type_mat 
          AND marche.id_mar=materiel.id_mar
            AND marche.annee =".$an;
          $result = mysqli_query($conn, $sql);
          if (mysqli_num_rows($result) > 0) {
                // Parcourir les lignes de résultat
              while($row = mysqli_fetch_assoc($result)) {

                       echo "<tr><td>".$row["libelle_mat"]
                      ."</td><td>".$row["num_inv"]
                      ."</td><td>".$row["num_serie"]
                      ."</td><td>".$row["libelle_type"]
                      ."</td><td>".$row["etat"]
                      ."</td><td>".$row["num_mar"]
                      ."</td><td><a href=\"modify.php?id_mat=".$row["id_mat"]."\"></a>"
                      ."</td><td><a href=\"supp.php?id_mat=".$row["id_mat"]."\" onclick=\"confirm('هل تريد حقا الحذف')\"></a></td><td><a href=\"showrepa.php?id_mat=".$row["id_mat"]."\"></a>"
                      ."</td><td><a href=\"bureaux.php?id_mat=".$row["id_mat"]."\"></a>"
                      ."</td></tr><br>";





            } 
          } 
        ?>
  </tbody>
</table></div>
</body>
</html>

Recommended Answers

print '<option value="'.$row ['annee'].'" >'.$row ['annee'].'</option>';

I think what rproffitt is referring to is the space between the array's variable name and the array index. It should be $row['annee'] with no space between the $row and the [.

Jump to Post

Undefined array key in php _post

You mention the error message is in the topic title, but typically PHP errors specify the line that the error is occurring on.

Jump to Post

All 8 Replies

Formatting could help.

  1. There's a glaring issue with line 24.
  2. Then we have lines 69 to 73.
  3. I have trouble finding the matches for the braces top to bottom.
  4. Line 49 is commented out so it may try to run the code to post every time.
  5. Line 49's bracket, since the line is commented out may have a stray bracket below.

The formatting is a mess so I'll stop here.

idk why u have found my code in a mess, for line 24 there are two braces one for the if statement and the second for the while statement, Unlike you I see that every brace is in the place he must be
thanks for ur reply

To me, this is poorly formatted so that stood out fast. Example at https://www.quora.com/How-important-is-formatting-code

I know some don't think it matters and some get upset about it. In school, such would be kicked back with either "try again", rejected or reduced grade. Here you are asking for help so put in the effort to present clean readable code.

Also, why is line 49 commented out?

PS. Dump variables before the line it fails at to check your work. Nod to https://www.w3schools.com/php/func_var_var_dump.asp

Thanks for these notes, I'll take them into account
and I will reform my code

print '<option value="'.$row ['annee'].'" >'.$row ['annee'].'</option>';

I think what rproffitt is referring to is the space between the array's variable name and the array index. It should be $row['annee'] with no space between the $row and the [.

commented: Thank you for this. +15

Undefined array key in php _post

You mention the error message is in the topic title, but typically PHP errors specify the line that the error is occurring on.

I correct the mistake between $row and brace, but I still have this error : Warning: Undefined array key "year" in C:\xampp\htdocs\Mat_Park\test.php on line 50, but when i add the statement if(isset($_POST["year"])) to chek if my variable is correctly recovered from the drop-down list,the error disappear but the information are not shown .
thanks for ur replies

So it looks like the 'year' parameter is not being passed into the form. You need to handle what you want to happen when if(isset($_POST["year"])) is returning false (in other words, it's NOT set), which is what it's doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.