ARTICLE AD BOX
After many hours of research I finally cracked the code to uploading images through POST and inserting them into sql.
index.php:
connect DB file to your index require "db.php";(prevents code from running when page is just opened)
if ($_SERVER["REQUEST_METHOD"] === "POST") {index.php:
gets the original name of the uploaded file, the path where it's stored & moves it to /uploads in your directory
$image = $_FILES["image"]["name"]; move_uploaded_file($_FILES["image"]["tmp_name"], "uploads/" . $image);prepears query
$stmt = $sql->prepare(" INSERT INTO db (image) VALUES (?) ");replaces ? with $filename
inserts filename into database
redirects back to index and stops from sending form again
$stmt->execute([$image]); header("Location: index.php"); exit; } ?>$stmt executes an SQL query, gets all db rows, shows query results, fetches in associative array
<?php $stmt = $sql->query("SELECT * FROM db"); $db = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>HTML table with php loop that goes through every DB row and each record is on new table cell
img src from uploads folder and the filename from your DB
<table border="1"> <tr> <th>Image</th> </tr> <?php foreach ($db as $d): ?> <tr> <td> <img src="uploads/<?= $d["image"] ?>" width="80"> </td> </tr> <?php endforeach; ?> </table> </body> </html>required db.php connection image(VAR255, null)
<?php $sql = new PDO("mysql:host=localhost;dbname=db;charset=utf8mb4", "root", "root"); ?>