I'm trying to create a table with data from a database
I'm trying to create a table with data from a database
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>";
This error occurs when you try to access a nonexistent index from 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 test.php on line 2
To avoid this error, always make sure 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 in the following way, using the Ternary Conditional Operator:
$pag = isset( $_GET['pag'] ) ? $_GET['pag'] : 'valor_padrao';
NEVER USE SPACES , NOR ACCENTS AND PREFERENCE WITHOUT BOX FOR COLUMN TITLES OF YOUR TABLE.
You have DESCRIÇÃO TAREFA, PENDENTES, DURAÇÃO TAREFA
that are 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>";
..
Hello,
First, it indicates that you do not use accents, cedilla, and spaces in your database for best practices.
To solve this problem, give var_dump($row)
within the while
loop to see how the fields are being returned, then update the indexes.
This error:
Notice: Undefined index: PENDENTES in C:\xampp\htdocs\reghoras\index.php on line 29
is related to the typing error, its field in the database is named as pedentes
and not peNdentes
.
Regarding the other errors, try running this line of code before the query so that the database is in the utf-8 format.
mysqli_query($dbcon, "SET CHARSET UTF8");
Abs.