PHP+MYSQL file filter

Asked

Viewed 76 times

0

Good afternoon this is my first post here, so I apologize if I’m repeating a question, let’s go to error have a registration application that I’m trying to implement file upload,below follows the PHP code that is doing the correct function:

<?php
//conectar ao BD.
$conn = mysqli_connect("localhost", "root", "", "gestao-unical");
if($conn) {
//conexao estabelecida.
echo "conectado";
}
//se o botão com o nome SendCadImg foi clicado
if(isset($_POST['SendCadImg'])) {
//declarando as variaveis
$filename = $_FILES['arquivos']['name'];
$filetmpname = $_FILES['arquivos']['tmp_name'];
$idEvento = $_POST['campoIDEVENTO'];
//pasta para onde vai os arquivos
$folder = 'arquivos/';
//função para salvar as imagens carregadas em uma pasta específica
move_uploaded_file($filetmpname, $folder.$filename);
//insert da imagem (nome da imagem) no banco de dados
$sql = "INSERT INTO `uploadedimage` (`imagename`,`idEvento`)  VALUES ('$filename','$idEvento')";
$qry = mysqli_query($conn,  $sql);
if( $qry) {
echo "</br>image uploaded"; 
}
}
?>

to list the files use the code below, but without reference to what was written in MYSQL

<?php
$pasta = 'arquivos/';

if(is_dir($pasta))
{
$diretorio = dir($pasta);

while(($arquivo = $diretorio->read()) !== false)
{
echo '<a href='.$pasta.$arquivo.'>'.$arquivo.'</a><br />';
}

$diretorio->close();
}
else
{
echo 'A pasta não existe.';
}
?>

how to take advantage of Insert and list the files by the ID generated in the database?

2 answers

0

Good from what I understand you would like to list the names of the recorded images for the user to click and open the image.

In this case you would make a simple query in the Database and list the results Simply put, it would look like this:

//Monta a pasta e valida se a mesma existe
$pasta = 'arquivos/';
if(!is_dir($pasta)){
   //caso não exista escreve a mensagem e para o codigo
   echo 'A pasta não existe.';
   exit;
}

//Sql e a função para executalo
$sql = "SELECT * FROM uploadimagens";
$result = mysqli_query($conn, $sql);

//Le os registros retornados na consulta
while ($row = mysqli_fetch_assoc($result)) {
   echo "<a href='{$pasta}{$row['imagename']}'>{$row['imagename']} {$row['idEvento']}</a><br />";
}
  • thanks for the answer , Oce understood what I want to do , I got your code but did not understand the Wile , I tried to use it and does not return anything on the screen.

  • I’m sorry I tried to replace the mysqli_fetch_assoc($result) for mysqli_fetch_array($result, MYSQLI_ASSOC) and do not forget to include the Mysql connection at the beginning of the program. while is to read all records that return in your query, as it can return more than one line

-1

you can make a query to DB with:

 QUERY * FROM `uploadedimage`SORT BY `nome_da_chave_primaria` 

Query, store in an array with mysqli_fetch_assoc.

Then iterate over the array showing the data the way you want it.

Roughly speaking it could look something like this:

$sql = "QUERY * FROM `uploadedimage`SORT BY `nome_da_chave_primaria`";
$qry = mysqli_query($conn, $sql);

$arr = mysqli_fetch_assoc($qry);

while ($arr) {
 echo $arr['imagename'] . " " . $arr['idEvento'];
}
  • did not understand how to proceed

Browser other questions tagged

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