Resolver undifined index

Asked

Viewed 245 times

1

I’m trying to create one table with data from a database , but when I open the page, it shows some results and then above appears this error:

Notice: Undefined index: TASK DESCRIPTION in C: xampp htdocs reghoras index.php on line 25

Notice: Undefined index: DURATION TASK in C: xampp htdocs reghoras index.php on line 28

Notice: Undefined index: PENDENTES in C: xampp htdocs reghoras index.php on line 29

Image of the database structure:

Imagem da estrutura da base de dados

Time Log

<body>
    <center><h2>Registo de Horas 2016</h2></center>
    <br>
    <?php
    mb_internal_encoding("UTF-8");
        include('connect_mysql.php');

        $sqlget = "SELECT * FROM `bdreghoras` where id !=0";
        $sqldata = mysqli_query($dbcon, $sqlget) or die('erro a obter os dados');

        echo "<table border= '1' cellpadding= '10'>";
        echo "<tr><th>Processo</th><th>Data (dd/mm/aaaa)</th><th>Utilizador</th><th>Descrição da Tarefa</th><th>Hora de inicio</th><th>Hora de Fim</th><th>Duração de Tarefa</th><th>Pendentes</th><th>Local</th></tr>";

        while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
            echo "<tr>";
            echo "<td>" . $row['PROCESSO'] . "</td>";
            echo "<td>" . $row['DATA'] . "</td>";
            echo "<td>" . $row['UTILIZADOR'] . "</td>";
            echo "<td>" . $row['DESCRIÇÃO TAREFA'] . "</td>";
            echo "<td>" . $row['HORA INICIO'] . "</td>";
            echo "<td>" . $row['HORA FIM'] . "</td>";
            echo "<td>" . $row['DURAÇÃO TAREFA'] . "</td>";
            echo "<td>" . $row['PENDENTES'] . "</td>";
            echo "<td>" . $row['LOCAL'] . "</td>";
            echo "</tr>"; 
        }
        echo "</table>";
    ?>
</body>

I don’t know how to fix it I need help

  • can post your PHP code, and the table structure? This helps a lot to identify the problem. Welcome to SOPT :)

  • Are you sure that in all rows these three columns have value?

  • the problem is in the columns Memory because they have space, try to use a var_dump to see how the names are being displayed.

  • 1

    it is highly recommended not to use accented characters and spaces in the seat structure, as these can be converted differently.

3 answers

3

First of all, the structure of your table should be as follows::

CREATE TABLE `bdreghoras` (
  `ID` INT NOT NULL AUTO_INCREMENT,
  `PROCESSO` VARCHAR(50) NOT NULL,
  `DATA` DATE NOT NULL,
  `UTILIZADOR` VARCHAR(2) NOT NULL,
  `DESCRICAO_TAREFA` VARCHAR(250) NOT NULL,
  `HORA_INICIO` TIME NOT NULL,
  `HORA_FIM` TIME NOT NULL,
  `DURACAO_TAREFA` VARCHAR(5) NOT NULL,
  `PEDENTES` VARCHAR(50) NOT NULL,
  `LOCAL` VARCHAR(50) NOT NULL);

Then you should change your PHP script as follows:

// ...
while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
    echo "<tr>";
    echo "<td>" . $row['PROCESSO'] . "</td>";
    echo "<td>" . $row['DATA'] . "</td>";
    echo "<td>" . $row['UTILIZADOR'] . "</td>";
    echo "<td>" . $row['DESCRICAO_TAREFA'] . "</td>";
    echo "<td>" . $row['HORA_INICIO'] . "</td>";
    echo "<td>" . $row['HORA_FIM'] . "</td>";
    echo "<td>" . $row['DURACAO_TAREFA'] . "</td>";
    echo "<td>" . $row['PENDENTES'] . "</td>";
    echo "<td>" . $row['LOCAL'] . "</td>";
    echo "</tr>"; 
}
echo "</table>";

2

Undefined Index

This error occurs when trying to access a non-existent index of an array. This error is very common with beginners using Query String. The famous example:

<?php
$pag = $_GET['pag'];
?>

If there is no "pag" variable in the URL, it will give this error:

PHP Notice: Undefined index: pag in teste.php on line 2

To avoid this error, always check if the index exists. The isset function easily solves this problem:

if ( isset( $_GET['pag'] ) )
{
    $pag = $_GET['pag'];
}
else
{
    $pag = 'valor padrão';
}

The code can be rewritten as follows, using the ternary conditional operator:

$pag = isset( $_GET['pag'] ) ? $_GET['pag'] : 'valor_padrao';

YOUR PROBLEM:

SHOULD NEVER BE USED SPACES, NOR ACCENTS AND PREFERABLY WITHOUT HIGH CASH FOR COLUMN TITLES OF YOUR TABLE.

You have DESCRIÇÃO TAREFA, PENDENTES, DURAÇÃO TAREFA not being found in the variables:

...                   POR QUE???????

            //Erro de Acento na hora da conversão
            echo"<td>" . $row['DESCRIÇÃO TAREFA'] . "</td>";

            //No banco ta PEdente
            echo "<td>" . $row['PENDENTES'] . "</td>";

            //Erro de Acento na hora da conversão
            echo "<td>" . $row['DURAÇÃO TAREFA'] . "</td>";
..

0

Hello,

first indication that it does not use accents, cedilla and spaces in its database for good practices.

To solve this problem, give a var_dump($row) within the loop while to see how the fields are being returned, after that, update the indexes.

This mistake: Notice: Undefined index: PENDENTES in C:\xampp\htdocs\reghoras\index.php on line 29 is related to typo, your field in the database is named as pedentes and not peNdentes.

For other errors, test run this line of code before the query so that the database is in utf-8 format.

mysqli_query($dbcon, "SET CHARSET UTF8");

Abs.

Browser other questions tagged

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