PHP code just doesn’t start when loading the page!

Asked

Viewed 31 times

0

I made the code below to show the files that were inserted by the user, however it only shows the files after having attached a file, however I wanted it to show when loading the page, for the user to see which files he has already uploaded. This is the code I used to upload the file:

Lesson plan 1

    $arq = str_replace(" ", "_", $arq);
    $arq = str_replace("ç", "c", $arq);

    if (file_exists("uploads/".$arq)) {
      $a = 1;

      while (file_exists("uploads/[".$a."]".$arq)) {
        $a++;
      }

      $arq = "[".$a."]".$arq;
    }

    if (move_uploaded_file($_FILES['arquivo']['tmp_name'], 'uploads/'.$arq)) {
      $objDb = new db();
      $link = $objDb->conecta_mysql();
      $sql = "insert into arquivos (email_vol, nomearq) values ('".  $email."', '".$arq."')";
      if (mysqli_query($link, $sql)){
        echo 'Plano de aula 1 enviado com sucesso!';
      } else {
        echo (mysqli_error($link));
        echo 'Erro ao enviar o plano de aula!';
      }

    } else {
      echo "Nenhum arquivo selecionado!";
    }

  }
  ?>
</p></center>

This is the code to show the file on the page:

<div class="container">
  <div class="text-center">
       <h1 class="tittlenoticia" style="color: black;">Arquivos anexados</h1>
       <?php
       require_once('conecta.php');
       $pasta = "uploads/";
       $consulta = mysqli_query($link, "SELECT * FROM arquivos WHERE email_vol = '$email'");
        while ($resultado = mysqli_fetch_array($consulta)) {
            echo "<a href=\"" . $pasta . $resultado["nomearq"] . "\">" . $resultado["nomearq"] . "</a><br />";
        } 
       ?>

  </div>
</div>

Can someone give me a hand? Thanks in advance.

  • It’s not very nice to run a query within the view. You need to separate the responsibilities. This makes it easier even when debugging your application. You can work with MVC, or structured with MV only, without the controller. There are several patterns... but please, query in view is a bad practice.

  • 1

    @Thiagocunha I really do not know this MVC, I started learning php now.

  • I understand! Put at least your querys in your business model, that is, in Model. I found this question that should be of great help: https://answall.com/questions/67699/view-project-mvc-em-php

  • @Thiagocunha Just to my understanding, this would do in a file the whole page view, and in a separate file do the php codes, and call these codes in the view?

No answers

Browser other questions tagged

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