"Notice: Undefined index: matricula" error

Asked

Viewed 442 times

-1

I have two identical codes. One works (user) and one doesn’t (cars). Someone can explain why?

inserir a descrição da imagem aqui Code for the cars:

<html>
<head>
<meta charset="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>

MISTAKES:

Notice: Undefined index: matricula in C: Program Files Easyphp-Devserver-14.1VC11 data localweb Module 7 - Work N5 edita_elimina_carronew.php on line 37

This error occurs several for all items in the code.

inserir a descrição da imagem aqui

  • probably the table cars does not have the attribute license plate of a conferred

  • It gives the same error for all attributes.. :/

  • 1

    put the structure of the two tables, put only the code of the file edita_elimina_carronew.php the files that have no error do not need

  • I’ve already edited the question.

1 answer

2


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

For example, your table carros has the field Matricula, soon the key in the array will be Matricula and not matricula lower-case.

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

Make this correction 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++;
            }
        }
    ?>

  • Warning: Illegal string offset 'matricula' Do this error :/

  • Warning: mysql_fetch_array() expects Parameter 1 to be Resource, Object Given in C(...)

  • I’ve changed. Now you’ve made the same mistake from the beginning..

  • Ya ok. That was the problem...... I had an idea that being big or small letter didn’t matter. Thanks :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.