"Notice: Undefined index: enrollment" error

-1

I have two identical codes. One works (user) and another does not (cars). Can someone explain to me why?

Codeforcars:

<html><head><metacharset="utf-8">
<title>Eliminar</title>
</head>
<body>

<?php
    include("validar.php");
    include("ligaBD.php");

    $procura = "SELECT * FROM carros WHERE matricula LIKE '%".$_GET["mat"]."%'";
    $faz_procura = mysqli_query($ligaBD, $procura);
    $num_registos = mysqli_num_rows($faz_procura);

    if($num_registos==0)
    {
        echo "Não foram encontrados registos. ";
        echo "<a href='edita_elimina_carro.html'>Faça nova pesquisa";
        exit;
    }else
    {
        echo "<table border=0 width=300px><tr bgcolor=red>";
        echo "<th>Nome</th><th>Idade</th><th>Email</th><th>Username</th>";
        echo "<th>Password</th><th>Apagar</th></tr>";

        for($i=0; $i<$num_registos; $i++)
        {
            $registos = mysqli_fetch_array($faz_procura);
            if($i & 1)
            {
                echo '<tr bgcolor=yellow>';
            }else
            {
                echo '<tr bgcolor=grey>';
            }
            echo '<td>'.$registos['matricula'];
            echo '<td>'.$registos['user'];
            echo '<td>'.$registos['marca'];
            echo '<td>'.$registos['ano'];
            echo '<td>'.$registos['celindrada'];
            echo '<td><a href="apaga_carro.php?nome='.$registos['matricula'].'">Elimina</a>';
            echo '</td>';
        }
    }
?>

</body>
</html>

Other:

<html>
<head>
<meta charset="utf-8">
<title>Eliminar</title>
</head>
<body>

<?php
    include("validar_admin.php");
?>

<h3>Eliminar Utilizadores</h3>
<form method="GET" name="form1" action="edita_elimina_carronew.php">
<input type="text" name="mat">
<input type="submit" value="Pesquisar">
</form>

</body>
</html>

ERRORS:

  

Notice: Undefined index: Enroll in C: \ Program Files \ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ Module 7 - Work n5 \ edita_elimina_carronew.php online 37

This error occurs several times for all items in the code.

    
asked by anonymous 25.02.2016 / 17:37

1 answer

2

When you use the mysqli_fetch_array function, an array with indices is generated with exactly the same name as the fields in your table.

For example, your carros table has the Matricula field, so the key in the array will be Matricula and not matricula in lowercase.

The Notice: Undefined index: matricula... error occurs because in PHP the indices of an array are case sensitive. A hash is generated for each index, so a hash with letter "M" is different from a hash with the letter "m".

Make this fix in your code:

    <?php
        include("validar.php");
        include("ligaBD.php");

        $procura = "SELECT * FROM carros WHERE matricula LIKE '%".$_GET["mat"]."%'";
        $faz_procura = mysqli_query($ligaBD, $procura);
        $num_registos = mysqli_num_rows($faz_procura);

        if($num_registos==0) {
            echo "Não foram encontrados registos. ";
            echo "<a href='edita_elimina_carro.html'>Faça nova pesquisa";
            exit;
        } else {
            echo "<table border=0 width=300px><tr bgcolor=red>";
            echo "<th>Nome</th><th>Idade</th><th>Email</th><th>Username</th>";
            echo "<th>Password</th><th>Apagar</th></tr>";

            $i = 0;
             while($registos = mysqli_fetch_array($faz_procura,MYSQLI_ASSOC)) {

                if($i & 1) {
                    echo '<tr bgcolor=yellow>';
                } else {
                   echo '<tr bgcolor=grey>';
                }
                echo '<td>'.$registos['Matricula'];
                echo '<td>'.$registos['User'];
                echo '<td>'.$registos['Marca'];
                echo '<td>'.$registos['Ano'];
                echo '<td>'.$registos['Celindrada'];
                echo '<td><a href="apaga_carro.php?nome='.$registos['Matricula'].'">Elimina</a>';
                echo '</td>';
                $i++;
            }
        }
    ?>

    
25.02.2016 / 18:00