Does Mysqli only execute if it has a submit?

0

Hello, I have a code, explained below, where I want to make available the files that users uploaded for download, so I made a php code, but the query is only executed if I upload a file, but I wanted it to execute when loading the page, does anyone know some way to make the query run at the start of the page or if something is wrong with my code? Thank you!

First I made the connection to the database, as shown below:

<?php

class db {
//host
private $host = 'localhost';

//usuario
private $usuario = 'iddsafdsfr';

//senha
private $senha = 'fasdfasd';

//banco de dados
private $database = 'fasdfasdg';

public function conecta_mysql(){

    //criar a conexão
    $con = mysqli_connect($this->host, $this->usuario, $this->senha, $this->database);

    //ajustar a charser de cominicação entre a aplicação e o bd
    mysqli_set_charset($con, 'utf8');

    //verificar se houve erro de conexão
    if (mysqli_connect_errno()) {
        echo 'Erro ao tentar se conectar com o banco de dados'.mysqli_connect_error();
    }

    return $con;
}
}
?>

With the connection to the database made, I created the table directly in phpmyadmin and did the form part, where the user would insert the files, as follows in the code below:

<div class="container">
  <div class="text-center">
    <center><h1>Plano de aula 1</h1><form method="post" enctype="multipart/form-data" action="">
      <input type="file" name="arquivo" />
      <input type="submit" class="btn btn-primary" value="enviar arquivo" name="enviar">
    </form></center>
  </div>
  </div>
<center><p class="text-success" style="font-size: 20px; padding-bottom: 100px;">
  <?php
  session_start();
  require_once('conecta.php');
  $email = $_SESSION['email'];
  if (isset($_POST['enviar'])) {
    $arq = $_FILES['arquivo']['name'];
    $dataup = date('Y-m-d H:i:s');
    echo $dataup;

    $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!";
    }

  } else{

  }
  ?>
</p></center>

And lastly I did the part where it was to show all the files that were uploaded according to the email and made available for download. But as I said before, the query $consulta = mysqli_query($link, "SELECT * FROM arquivos WHERE email_vol = '$email'"); is only executed if a new upload is done, however I would like it to be executed when loading the page, so that the user can see what files he has uploaded.

<?php
      session_start();
      $email = $_SESSION['email'];
      echo $email;
      require_once('conecta.php');
      $pasta = "uploads/";
      $consulta = mysqli_query($link, "SELECT * FROM arquivos WHERE email_vol = '$email'");
      var_dump($consulta);
      while ($resultado = mysqli_fetch_array($consulta)) {
        echo "<a href=\"" . $pasta . $resultado["nomearq"] . "\">" . $resultado["nomearq"] . "</a><br />";
} 
?>
    
asked by anonymous 25.10.2017 / 22:47

0 answers