写真をアップロードとMYSQLへINSERT文でテーブルにデータを追加

style.css

.btn {
    border: 2px solid black;
    border-radius: 5px;
    background-color: white;
    color: black;
    padding: 14px 28px;
    font-size: 16px;
    cursor: pointer;
  }
  .info {
    border-color: #2196F3;
    color: dodgerblue
  }
  .info:hover {
    background: #2196F3;
    color: white;
  }
  .default {
    border-color: #e7e7e7;
    color: black;
  }
  .default:hover {
    background: #e7e7e7;
  }
  .warning:hover {
    background: #ff9800;
    color: white;
  } 
  div.gallery {
    border: 1px solid #ccc;
  }
  div.gallery:hover {
    border: 1px solid #777;
  }
  div.gallery img {
    width: 100%;
    height: 300px;
    object-fit: cover;
  }
  
  div.desc {
    padding: 15px;
    text-align: center;
  }
  
  * {
    box-sizing: border-box;
  }
  span{
      margin-right: 10px;
      font-weight: bold;
      color: #777;
  }
  .responsive {
    padding: 0 6px;
    float: left;
    width: 24.99999%;
  }
  
  @media only screen and (max-width: 700px) {
    .responsive {
      width: 49.99999%;
      margin: 6px 0;
    }
  }
  
  @media only screen and (max-width: 500px) {
    .responsive {
      width: 100%;
    }
  }
  
  .clearfix:after {
    content: "";
    display: table;
    clear: both;
  }

index.php

db_conn.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "image_db";


$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
データベースとテーブルの作成

データベース名:image_db
テーブル名:images

テーブル構造

Snow

upload.php

<?php
include 'db_conn.php';
if (isset($_POST['submit'])) {
    $file = $_FILES['file'];
  
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png', 'pdf');

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0 ) {
            if ($fileSize < 1000000) {
                $fileNameNew = "Img_".date("Ymd_").bin2hex(random_bytes(2)).".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                
                $sql = "INSERT INTO  images (name) VALUES ('$fileNameNew')";
                mysqli_query($conn,$sql);
                $em =  $fileName."をアップロードしました";
                header("Location: index.php?info=$em");
            } else {
                $em = $fileName."のファイルが大きすぎます";
                header("Location: index.php?info=$em");
            }
        } else {
                $em = "アップロードにエラーが発生しました";
                header("Location: index.php?info=$em");
        }
    } else {
                $em = "ファイルの種類が対象外です。アップロードできません。";
                header("Location: index.php?info=$em");
    }
   
}