Currently i’m working on making a site that uploads multiple image files and shows the images uploaded individually according to Project ID in another page
for now i made a MySQL DB and have been using it to register the “projects” and save the file path in another table while the php script is responsible for uploading the file path of each image directly onto the db, but for now i’m a little confused in how to make the images show up individually while pulling the file path from the DB
Here’s my current code for now
SQL
CREATE TABLE projetos(
id INT(11) auto_increment PRIMARY KEY,
nome TEXT,
turma TEXT(10),
camArquivo TEXT
);
CREATE TABLE imagens (
id INT(11) auto_increment primary key,
id_projeto INT (11),
camArquivo TEXT,
FOREIGN KEY (id_projeto) REFERENCES projetos(id)
);
PHP
inserir_db.php
<?php
include_once 'connect.php';
//Saving text inputs into PHP Variables
$nome = isset($_POST['nome']) ? $_POST['nome'] : null;
$turma = isset($_POST['turma']) ? $_POST['turma'] : null;
//Creating the condition variables for the upload
$uploadimgOK = 0;
$uploadsuccess = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//
$target_dir = 'uploads/';
$allowTypes = array('jpg', 'png', 'jpeg', 'gif');
$fileNames = array_filter($_FILES['files']['name']);
// Uploading text fields into the table so that it generates the ID used for
// registering each image into the Images Table
$stmt = $pdo->prepare('INSERT INTO projetos (nome, turma) VALUES (:nome, :turma)');
$stmt->execute([':nome' => $nome, ':turma' => $turma ]);
$id = $pdo->lastInsertId();
if (!empty($fileNames)) {
foreach ($fileNames as $key => $value) {
$fileName = basename($_FILES['files']['name'][$key]);
$targetFilePath = $target_dir . $fileName;
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
//Verifying the file extension so that it is really a image file
if (in_array($fileType, $allowTypes)) {
$uploadimgOK= 1;
}else{
$uploadimgOK= 0;
}
// Uploading image file path into MySQL
if ($uploadimgOK == 1) {
$stmt = $pdo->prepare('INSERT INTO imagens(id_projeto, camArquivo) VALUES (:id, :arquivo)');
$stmt->execute([':id' => $id, ':arquivo' => $targetFilePath]);
}else{
echo 'error while uploading image';
}
// Uploading image into Server
if ($uploadimgOK == 1) {
move_uploaded_file($_FILES['files']['tmp_name'][$key], $targetFilePath);
$uploadsuccess = 1;
}else{
echo 'error while uploading project';
$uploadsuccess=0;
}
}
// After project is sucessfully uploaded, it redirects to the next page
if ($uploadsuccess == 1) {
header('Location: testesucesso.html');
exit();
}
}
}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Project Upload</title>
</head>
<body>
<h2>Upload Projects</h2>
<form action="inserir_db.php" method="POST" enctype="multipart/form-data">
<div>
<label for="project1Name">Project Name:</label>
<input type="text" id="nome" name="nome">
<br>
<label for="project1Description">Turma:</label>
<textarea id="turma" name="turma" rows="4" cols="50"></textarea>
<br>
<label for="project1Image1">Image 1:</label>
<input type="file" id="img" name="files[]" accept="image/*" multiple>
<br>
<br><br>
</div>
<input type="submit" value="UPLOAD">
</form>
</body>
<script type="javascript">
</script>
</html>
Aozakiii is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.