PHP How To Upload Image in MySQLi Database Tutorial

Reading Time: 7 minutes

Inside this article we will see the concept i.e PHP How to upload image in mysqli database. Tutorial contains the classified information about uploading form data with image in PHP. We will use mysqli connection for database connectivity and operations.

If you are looking for an article to understand the image upload process with form data then this article will be super easy to understand. This will give you the complete idea about form submission with image file in PHP MySQLi.

Learn More –

Let’s get started.

Create Database & Table

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE php_applications;

Inside this database, we need to create a table.

Table we need – students

CREATE TABLE `students` (
 `name` varchar(120) NOT NULL,
 `email` varchar(120) NOT NULL,
 `profile_image` varchar(220) NOT NULL,

Application Folder Structure

You need to create a folder structure to develop this application in PHP and MySQLi. Have a look the files and folders inside this application –

Create a folder with name php-form-with-image-upload and create these 2 files into it. And also create a uploads/ folder. This is to store images. Make sure this folder should have sufficient permission to read and write files into it.

Database Configuration

Open dbconfig.php file from folder. Add these lines of code into it.

 @Author: Sanjay Kumar
 @Project: PHP How To Upload Image in MySQLi Database Tutorial

// Database configuration
$host   = "localhost";
$dbuser = "admin";
$dbpass = "Admin@123";
$dbname = "php_applications";

// Create database connection
$conn = new mysqli($host, $dbuser, $dbpass, $dbname);

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

Application Programming

Open index.php file and write this following code into it.

 @Author: Sanjay Kumar
 @Project: PHP How To Upload Image in MySQLi Database Tutorial

// Include the database configuration file 
require 'dbconfig.php';

if (isset($_POST['submit'])) {

    if (empty($_POST['name'])) {
        $errors['name'] = "*The name field is required.";

    if (empty($_POST['email'])) {
        $errors['email'] = "*The email field is required.";

    if (preg_match("/\S+/", $_FILES['image']['name']) === 0) {
        $errors['image'] = "*The image field is required.";

    $filename = $_FILES['image']['name'];

    $imageFileType = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

    $extensions_arr = array("jpg", "jpeg", "png");

    if (in_array($imageFileType, $extensions_arr)) {

        if (move_uploaded_file($_FILES["image"]["tmp_name"], 'uploads/' . $filename)) {

            $stmt = $conn->prepare("INSERT INTO students (name, email, profile_image) VALUES (?, ?, ?)");
            $stmt->bind_param("sss", $name, $email, $filename);

            $name = $_POST['name'];
            $email = $_POST['email'];


            $successful = '<h5 class="alert alert-success">Data saved successfully!</h5>';
        } else {
            echo 'Error in uploading file - ' . $_FILES['image']['name'] . '<br/>';


<!DOCTYPE html>
<html lang="en">

    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>


    <div class="container">
        <h4>PHP How To Upload Image in MySQLi Database Tutorial</h4>
        <div class="panel panel-primary">
            <div class="panel-heading">PHP How To Upload Image in MySQLi Database Tutorial</div>
            <div class="panel-body">
                <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
                    <?php if (isset($successful)) {
                        echo $successful;
                    } ?>

                    <div class="form-group">
                        <div class="custom-file">
                            <label for="customFile">Name</label>
                            <input type="text" name="name" id="name" class="form-control">

                            <?php if (isset($errors['name'])) {
                                echo "<span class='text-danger'>" . $errors['name'] . "</span>";
                            } ?>
                    <br />
                    <div class="form-group">
                        <div class="custom-file">
                            <label for="customFile">Email</label>
                            <input type="text" name="email" id="email" class="form-control">

                            <?php if (isset($errors['email'])) {
                                echo "<span class='text-danger'>" . $errors['email'] . "</span>";
                            } ?>

                    <br />
                    <div class="form-group">
                        <div class="custom-file">
                            <label for="customFile">Profile Image</label>

                            <input type="file" name="image" id="customFile" class="form-control">

                            <?php if (isset($errors['image'])) {
                                echo "<span class='text-danger'>" . $errors['image'] . "</span>";
                            } ?>


                    <div class="form-group d-flex">
                        <input type='submit' name='submit' value='Submit' class="btn btn-primary px-4">




Application Testing


URL: http://localhost/php-form-with-image-upload/index.php

Submit form without any value

Submit form with value

You should see inside database table, records as –

Also, the profile_image files of students will be uploaded to uploads/ directory.

Download Complete Source Code

We hope this article helped you to learn PHP How To Upload Image in MySQLi Database Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more