Error: Undefined variable 'x' in'local

Asked

Viewed 381 times

-7

I’m learning PHP, on the page appears that:

"Notice: Undefined variable: tasks in C: xampp htdocs index.php on line 42."

Código PHP:
<?php

$bdServidor = '127.0.0.1';
$bdUsuario = 'felipe';
$bdSenha = 'testeteste123' ;
$bdbanco = 'tarefas' ;

$conexao = mysqli_connect($bdServidor,$bdUsuario,$bdSenha,$bdbanco);

if (mysqli_connect_errno($conexao)) 
{
echo "Problemas para conectar no banco. Verifique os dados!";
}
else
{
  echo "tudo ocorreu bem";
}
function busca_tarefas($conexao)
{
    $sqlBusca = 'SELECT * FROM tarefas';
    $resultado = mysql_query($conexao,$sqlBusca);
    $tarefas= array();
    while($tarefa =  mysqli_fetch_assoc($resultado)) 
    {
        $tarefas[] = $tarefa;
    }
    return $tarefas;
}
?>

Código HTML:
<?php
session_start();
include "tarefas.php";
?>
<!DOCTYPE html>
<html>
<head>
    <title>Tarefas</title>
    <link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<center><form method="GET">
    <fieldset id="felipe">
        <legend>Nova tarefa</legend>
        <label>Nome
            <input class="theme" type="text" name="nome">
        </label><br>
        <label>descrição
            <textarea class="themee" name="descricao"></textarea>
        </label><br>
        <label>Prazo
            <input class="theme" type="text" name="prioridade">
        </label><br>
        <fieldset id="felipee">
            <legend>Prioridade</legend>
        <label>
            <input type="radio" name="alta">Alta
            <input type="radio" name="media">Média
            <input type="radio" name="baixa">Baixa
        </label><br>
        </fieldset><br>
        <input type="submit" name="enviar">
    </fieldset>
</form></center>
     <table border='1px'>
        <tr>
            <td>Nome</td>
            <td>descricao</td>
            <td>prioridade</td>
        </tr>
        <tr>
            <td><?php print_r($tarefas)
            ?></td>
        </tr>
     </table>
</body>
</html>

How can I correct this mistake?

  • The error is on line 42 as you posted it yourself: "... Undefined variable: tasks in C: xampp htdocs index.php on line 42."...". Variable not defined in index.php at line 42

  • But how can I fix?

  • 1

    Please make an intuitive title for the question, write help please is totally redundant, everyone who are asking on the site are already looking for help, so it’s obvious, write a useful title setting the problem helps to increase people’s interest.

  • 1

    @Feliphestival Arrogancia is something quite different from saying the obvious, that’s what I said and with no intention of offending you, it’s typo or lack of learning the basics. I answered and explained how you use and the question of scope, to have someone "stick something in **" to fight back sounds like criticism is to act in a totally disproportionate and unnecessary way which shows that you still need to learn to take advantage of things, even the worst situation, and mature more to avoid being offended for free. So do not use insults and try to accept and understand the guidelines on codes.

1 answer

5

I think it should be instead of this:

 <td><?php print_r($tarefas)
        ?></td>

This:

 <td><?php
   $tarefas = busca_tarefas($conexao);
   print_r($tarefas);
   ?></td>

Because as far as I understand functions do not perform themselves and the variable $tasks is within the scope and in the return, that is to pass to a variable outside the scope of the function, $tarefas =.

It seems to me it was either some typo on your part or lack of understanding the basics of what a function is.

Browser other questions tagged

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