Error while listing database data

Asked

Viewed 44 times

1

You’re making the following mistake :

Notice: Undefined variable: denuncia in C: xampp htdocs Nise geral.php on line 262

Warning: Invalid argument supplied for foreach() in C: xampp htdocs Nise general.php on line 262

Line 262 is where this code snippet is: <?php foreach ( $denuncia as $den ) { ?>

.

    <?php 

if(!empty($_FILES['uploaded_file'])){
    $username = 'root';
    $password = '';
    $connection = new PDO( 'mysql:host=localhost;dbname=nise', $username );


    $query = "INSERT INTO resposta_denuncia (descricao_resposta, imagem, id_usuario) 
          VALUES (:descricao_resposta, :imagem, :id_usuario)";
    $statement = $connection->prepare($query);



    $path = "img_denuncia/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path))



    $valores = array();
    $valores[':descricao_resposta'] = $_POST['descricao_resposta'];
    $valores[':imagem'] = $_FILES['uploaded_file']['name'];
    $valores[':id_usuario'] = 2;


  if(!isset($_POST['descricao_resposta']) or empty($_POST['descricao_resposta'])) {

    echo 0;

}elseif($result = $statement->execute($valores)) {

    echo 1; // dados enviados com sucesso

} else {

    echo 0; // erro ao tentar enviar dados 

}

    //criei a query
        $query = "SELECT id, data, descricao, imagem, image FROM denuncia";

        //prepara a query
        $statement = $connection->prepare($query);

        //executar o comando sql
        $result = $statement->execute();

        //juntar todos os resultados do select em um vetor de arrays
        $statement->execute();
        $denuncia = $statement->fetchAll();

    return $denuncia;

}
?>

table

<!--Começo da tabela-->
            <div class="card mb-3">
             <div id="wrapper">
                <div id="content-wrapper">
                    <div class="container-fluid">
                        <div class="card-body">
                            <div class="table-responsive">
                    <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">

                      <thead>
                        <tr>
                            <th id="id_bd">Nº</th>
                            <th id="date">Data e hora</th>
                            <th id="local">Local</th>
                            <th id="comentario">Comentário</th>
                            <th id="imagem">Imagem</th>
                        </tr>
                      </thead>


                    <?php  foreach (  $denuncia as $den )  {  ?>
                        <tr>
                          <td><?php echo $den["id"];?></td>
                          <td><?php echo $den["data"];?></td>
                          <td><?php echo $den["descricao"];?></td>
                          <td><?php echo $den["imagem"];?></td>
                          <td><?php echo $den["qual_descricao"];?></td>
                        </tr>
                    <?php } ?>


                      </table>
                  </div>
                </div>
                <div class="card-footer small text-muted">Atualizado ontem às 11:59 PM</div>
              </div>
          </div>
        </div>
        </div>
        <!--Começo da tabela-->
  • 1

    The variable $denuncia was not defined. Where is it created? I don’t see it in your code.

  • You are using pure php or some framework, we need more details...

  • Jrd, so I forgot to create this variable

  • Alvaro, I’m wearing Bootstrap

1 answer

1


Actually, you should not use the variable $denuncia and yes the variable $result'.

<?php  foreach (  $result as $den )  {  ?>

$denuncia does not exist, unreported.

But the right thing would be this::

$statement->execute();
$denuncia = $statement->fetchAll();

// ...

<?php  foreach (  $denuncia as $den )  {  ?>

I say this because, in your code, redundantly you are putting the variable $result to receive the return of functions execute() and fetchAll().

  • So I must create this variable, $denounce, but where ? I put it under the $result = $statement->fetchAll(); and it keeps giving error.

  • In place of $result = $statement->fetchAll(); you will use $denuncia = $statement->fetchAll();

  • 1

    Below $denuncia = $statement->fetchAll(); place print_r($denuncia); exit; to see the return. So you can identify the possible error, and of course, always user Try/catch to capture exceptions and track logs through the terminal.

Browser other questions tagged

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