HTML forms won’t upload image to my PHP code (myphpadmin included)

I am having problems with my php code, I’ve been searching three days to solve this and still does not work and keeps showing this error on the PHP code:
Warning: Trying to access array offset on value of type null in C:xampphtdocsphp-projectsPatient.php on line 195

Fatal error: Uncaught ValueError: Path cannot be empty in C:xampphtdocsphp-projectsPatient.php:195 Stack trace: #0 C:xampphtdocsphp-projectsPatient.php(195): getimagesize(”) #1 {main} thrown in C:xampphtdocsphp-projectsPatient.php on line 195

This is the html code to submit the forms:

<!DOCTYPE html>

<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE-edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Mentcare IS</title>
    <meta name="author" content="Ryufath">
    <meta name="description" content="Our Official Restaurant Site">
    <link rel="stylesheet" href="home.css">
    <style>
        /* Do not forget to copy paste the header formats when there are new changes*/
        
        /* General styles for the entire page */
        html {
            background-color: white;
        }

        /* Header styles */
        header {
            display: flex;
            justify-content: center;
            justify-content: flex-end;
            color: rgb(254, 207, 207);
            align-items: center;
            text-align: center;
            background-color: #14d8a7;
            z-index: 1000; /* Ensure the header is on top of other elements */
            position: fixed;
            font-size: 14px;
            padding-top: 35px;
            padding-bottom: 35px;
            padding-right: 10px;
            padding-left: 10px;
            margin-right: 20%;
            width: 100%; /* Too make sure the header is filled/padded full from left to right */
            left: 0;
            right: 0;
            top: 0;
        }

        footer{
            padding:10px;
            size:5px;
            background-color: #14d8a7;
            color:white;
            text-align: left;
        }

        /* Navigation styles */
        .nav__links {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        .nav__links li {
            display: inline-block;
            margin-right: 20px;
            position: relative; /* Needed for the dropdown */
        }

        .nav__links li a {
            font-weight: bold;
            color: white; /* Header text color*/
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .nav__links li a:hover {
            color: #ffff4d;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #4D4D4F;
            min-width: 160px;
            box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2);
            z-index: 1;
        }

        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
        }

        .dropdown-content a:hover {
            background-color: #ffff4d;
            color: black;
        }

        .nav__links li:hover .dropdown-content {
            display: block;
        }

        .Home {
            width: 3%;
            height: 3%;
            position: fixed;
            top: 32px;
            left: 6px;
        }

        .form-container {
            margin-top: 100px;
            padding: 20px;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
            background-color: #f2f2f2;
            border-radius: 8px;
        }

        .form-container label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }

        .form-container input[type="text"], .form-container select {
            width: 100%;
            padding: 8px;
            margin-bottom: 20px;
            border-radius: 4px;
            border: 1px solid #ccc;
        }

        .form-container input[type="submit"] {
            background-color: #14d8a7;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .form-container input[type="submit"]:hover {
            background-color: #12c397;
        }
    </style>
</head>

<body>
    <header>
        <a href="index.html">
            <img class="Home" src="237-2378542_icon-symbol-gui-house-home-start-top-check-removebg-preview.png">
        </a>
        <nav>
            <ul class="nav__links">
                <li><a href="AboutUs.html">About Us</a></li>
                <li>
                    <a href="#">Patient Management</a>
                    <div class="dropdown-content">
                        <a href="AddNewPatient.html">Add New Patient</a>
                        <a href="http://localhost/php-projects/Patient.php">Find & Search Patient</a>
                    </div>
                </li>

                <li><a href="Appointments.html">Appointments</a></li> <!-- It will lead directly to calendar-->

                <li><a href="#">Medication Management</a>
                    <div class="dropdown-content">
                        <a href="AddMedication.html">Add Medication</a>
                        <a href="http://localhost/php-projects/medication.php">Medication List</a>
                    </div>
                </li>
                <li><a href="#">Reports</a></li>
                <li><a href="#">User Book</a></li>
            </ul>
        </nav>
    </header>

    <div class="form-container">
        <form action="C:xampphtdocsphp-projectsmedication.php" method="POST">
            <label for="name">Medication Name:</label>
            <input type="text" id="name" name="name" required>

            <label for="formulation">Formulation:</label>
            <select id="formulation" name="formulation" required>
                <option value="Tablet">Tablet</option>
                <option value="Capsule">Capsule</option>
                <option value="Liquid">Liquid</option>
                <option value="Injection">Injection</option>
            </select>

            <label for="class">Class of Medication:</label>
            <input type="text" id="class" name="class" required>

            <label for="manufacturer">Manufacturer:</label>
            <input type="text" id="manufacturer" name="manufacturer" required>

            <label for="manufacturer">Descriptions:</label>
            <input type="text" id="comment" name="comment" required>

            <input type="submit" value="Add Medication">
        </form>
    </div>

    <footer>
        <p>All Rights Reserved (c)</p>
    </footer>
</body>

</html>

This is the PHP code to process the forms and submit it to my myphpadmin database:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE-edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Mentcare IS - View Patients</title>
    <meta name="author" content="Ryufath">
    <meta name="description" content="Our Official Patient Management Site">
    <link rel="stylesheet" href="home.css">
    <style>
        /* General styles for the entire page */
        html {
            background-color: white;
        }

        /* Header styles */
        header {
            display: flex;
            justify-content: center;
            justify-content: flex-end;
            color: white;
            align-items: center;
            text-align: center;
            background-color: #14d8a7;
            position: fixed;
            font-size: 14px;
            padding-top: 35px;
            padding-bottom: 35px;
            padding-right: 10px;
            padding-left: 10px;
            margin-right: 20%;
            width: 100%;
            left: 0;
            right: 0;
            top: 0;
        }

        footer {
            padding: 10px;
            background-color: #14d8a7;
            color: white;
            position: fixed;
            bottom: 0;
            width: 100%;
            text-align: left;
        }

        /* Navigation styles */
        .nav__links {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        .nav__links li {
            display: inline-block;
            margin-right: 20px;
            position: relative; /* Needed for the dropdown */
        }

        .nav__links li a {
            font-weight: bold;
            color: white; /* Header text color*/
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .nav__links li a:hover {
            color: #ffff4d;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #4D4D4F;
            min-width: 160px;
            box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2);
            z-index: 1;
        }

        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
        }

        .dropdown-content a:hover {
            background-color: #ffff4d;
            color: black;
        }

        .nav__links li:hover .dropdown-content {
            display: block;
        }

        .Home {
            width: 3%;
            height: 3%;
            position: fixed;
            top: 32px;
            left: 15px;
        }

        /* Table styles */
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 80px 0 20px;
            position: fixed;
            top: 15px;
        }

        table, th, td {
            border: 1px solid #ddd;
            text-align: left;
        }

        th, td {
            padding: 12px;
        }

        th {
            background-color: #f2f2f2;
        }
    </style>
</head>

<body>
    <header>
        <a href="index.html">
            <img class="Home" src="237-2378542_icon-symbol-gui-house-home-start-top-check-removebg-preview.png">
        </a>
        <nav>
            <ul class="nav__links">
                <li><a href="AboutUs.html">About Us</a></li>
                <li>
                    <a href="#">Patient Management</a>
                    <div class="dropdown-content">
                        <a href="AddNewPatient.html">Add New Patient</a>
                        <a href="http://localhost/php-projects/Patient.php">Find & Search Patient</a>
                    </div>
                </li>
                <li><a href="Appointments.html">Appointments</a></li> <!-- It will lead directly to calendar-->
                <li><a href="#">Medication Management</a>
                    <div class="dropdown-content">
                        <a href="AddMedication.html">Add Medication</a>
                        <a href="http://localhost/php-projects/medication.php">Medication List</a>
                    </div>
                </li>
                <li><a href="#">Reports</a></li>
                <li><a href="#">User Book</a></li>
            </ul>
        </nav>
    </header>

    <main>
    <table>
        <tr>
            <th>Full Name</th>
            <th>Date of Birth</th>
            <th>Birth Place</th>
            <th>Gender</th>
            <th>Telephone</th>
            <th>Email</th>
            <th>Image</th>
        </tr>

        <?php
        // Database connection details
        $servername = "localhost";
        $username = "root";
        $password = "<PASSWORD>";
        $dbname = "MentcareDB";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        // Check if form data is submitted
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Handle the image upload
            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["photo"]["name"]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        
            // Check if image file is an actual image
            $check = getimagesize($_FILES["photo"]["tmp_name"]);
            if ($check !== false) {
                $uploadOk = 1;
            } else {
                echo "File is not an image.";
                $uploadOk = 0;
            }
        
            // Additional checks for file size, format, etc.
        
            if ($uploadOk == 1) {
                if (move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file)) {
                    // Insert the data into the database
                    $stmt = $conn->prepare("INSERT INTO patients (full_name, date_of_birth, birth_place, gender, telephone_code, telephone_number, email, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
                    $stmt->bind_param("ssssssss", $fullname, $dob, $birthplace, $gender, $telcode, $tel, $email, $target_file);
        
                    // Set parameters and execute
                    $fullname = $_POST['fullname'];
                    $dob = $_POST['dob'];
                    $birthplace = $_POST['birthplace'];
                    $gender = $_POST['gender'];
                    $telcode = $_POST['telcode'];
                    $tel = $_POST['tel'];
                    $email = $_POST['email'];
                    $stmt->execute();
        
                    echo "New patient added successfully.";
                } else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
        }        
        
        // Fetch data from the 'patients' table
        $sql = "SELECT * FROM patients";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $imagePath = $row["image"];
                $imagePath = !empty($imagePath) ? $imagePath : 'C:UsersASUSDownloadsButtersStotch.png'; // Default image if none uploaded

                echo "<tr>
                        <td>" . $row["full_name"] . "</td>
                        <td>" . $row["date_of_birth"] . "</td>
                        <td>" . $row["birth_place"] . "</td>
                        <td>" . $row["gender"] . "</td>
                        <td>" . $row["telephone_code"] . " " . $row["telephone_number"] . "</td>
                        <td>" . $row["email"] . "</td>
                        <td><img src='" . $imagePath . "' alt='Patient Photo' width='100'></td>
                    </tr>";
            }
        } else {
            echo "<tr><td colspan='8'>No records found :) Please fill from 'Add New Patient'</td></tr>";
        }

        // Close connection
        $conn->close();
        ?>


    </table>
</main>


    <footer>
        <p>All Rights Reserved (c)</p>
    </footer>
</body>

</html>

I keep asking google, ChatGPT, but still I can’t get no answer. Is there something wrong with my code? Or is there somethig wrong with my folder path that I cannot upload an image from my computer sicne it shows an error? Or is this some system privelege thing?

If you know the answer to solve error: Warning: Trying to access array offset on value of type null in C:xampphtdocsphp-projectsPatient.php on line 195

Fatal error: Uncaught ValueError: Path cannot be empty in C:xampphtdocsphp-projectsPatient.php:195 Stack trace: #0 C:xampphtdocsphp-projectsPatient.php(195): getimagesize(”) #1 {main} thrown in C:xampphtdocsphp-projectsPatient.php on line 195

Please let me know

New contributor

ryu_0210 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật