Skip to main content

PHP Insert Image In MySQL

PHP Insert Image In MySQL

In this article, we are going to discuss PHP Insert Image In MySQL. We are going to create image file upload using PDO in PHP. This will be easy if you read or follow our previous tutorial in PHP Inserting Data To MySQL.

Let’s start with:

Creating our Table

We are going to make our database.

  1. Open the PHPMyAdmin.
  2. Create a database and name it as "upload_image".
  3. After creating a database name, then we are going to create our table. And name it as "tbl_image".
  4. Kindly copy the code below.

  1. CREATE TABLE `tbl_image` (
  2. `tbl_image_id` INT(11) NOT NULL,
  3. `first_name` VARCHAR(100) NOT NULL,
  4. `last_name` VARCHAR(100) NOT NULL,
  5. `image_location` VARCHAR(100) NOT NULL

Creating the Form Field - Modal Form

  1. <!-- Modal -->
  2. <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  3. <div class="modal-header">
  5. <h3 id="myModalLabel">Add</h3>
  6. </div>
  7. <div class="modal-body">
  8. <form method="post" action="upload.php" enctype="multipart/form-data">
  9. <table class="table1">
  10. <tr>
  11. <td><label style="color:#3a87ad; font-size:18px;">FirstName</label></td>
  12. <td width="30"></td>
  13. <td><input type="text" name="first_name" placeholder="FirstName" required /></td>
  14. </tr>
  15. <tr>
  16. <td><label style="color:#3a87ad; font-size:18px;">LastName</label></td>
  17. <td width="30"></td>
  18. <td><input type="text" name="last_name" placeholder="LastName" required /></td>
  19. </tr>
  20. <tr>
  21. <td><label style="color:#3a87ad; font-size:18px;">Select your Image</label></td>
  22. <td width="30"></td>
  23. <td><input type="file" name="image"></td>
  24. </tr>
  25. </div>
  26. <div class="modal-footer">
  27. <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
  28. <button type="submit" name="Submit" class="btn btn-primary">Upload</button>
  29. </div>
  30. </form>
  31. </div>

Creating Database Connection

  1. <?php
  2. $conn = new PDO('mysql:host=localhost; dbname=upload_image','root', '');
  3. ?>

Creating Insert Statement

  1. <?php
  3. require_once ('db.php');
  5. if (isset($_POST['Submit'])) {
  7. move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
  8. $location=$_FILES["image"]["name"];
  9. $fname=$_POST['first_name'];
  10. $lname=$_POST['last_name'];
  12. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $sql = "INSERT INTO tbl_image (first_name, last_name, image_location)
  14. VALUES ('$fname', '$lname', '$location')";
  16. $conn->exec($sql);
  17. echo "<script>alert('Successfully Added!!!'); window.location='index.php'</script>";
  18. }
  19. ?>

Source Code for displaying data

  1. <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
  2. <div class="alert alert-info">
  3. <button type="button" class="close" data-dismiss="alert">&times;</button>
  4. <strong><i class="icon-user icon-large"></i>&nbsp;Data Table</strong>
  5. </div>
  6. <tr>
  7. <th style="text-align:center;">User Image</th>
  8. <th style="text-align:center;">FirstName</th>
  9. <th style="text-align:center;">LastName</th>
  10. </tr>
  11. </thead>
  12. <?php
  13. require_once('db.php');
  14. $result = $conn->prepare("SELECT * FROM tbl_image ORDER BY tbl_image_id ASC");
  15. $result->execute();
  16. for($i=0; $row = $result->fetch(); $i++){
  17. $id=$row['tbl_image_id'];
  18. ?>
  19. <tr>
  20. <td style="text-align:center; margin-top:10px; word-break:break-all; width:450px; line-height:100px;">
  21. <?php if($row['image_location'] != ""): ?>
  22. <img src="uploads/<?php echo $row['image_location']; ?>" width="100px" height="100px" style="border:1px solid #333333;">
  23. <?php else: ?>
  24. <img src="images/default.png" width="100px" height="100px" style="border:1px solid #333333;">
  25. <?php endif; ?>
  26. </td>
  27. <td style="text-align:center; word-break:break-all; width:300px;"> <?php echo $row ['first_name']; ?></td>
  28. <td style="text-align:center; word-break:break-all; width:200px;"> <?php echo $row ['last_name']; ?></td>
  29. </tr>
  30. <?php } ?>


Adding new user.

New User

After adding a user.

Display Data

And, that's all. Kindly click the "Download Code" button for a full source code. Enjoy coding. Thank you very much.

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted bybalaji rajendran (not verified)on Tue, 09/12/2017 - 22:56

i downloaded the soure the upload.php one file error in this code the error is"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'upload_image.tbl_image' doesn't exist' in C:\xampp\htdocs\Upload-Image\upload.php:33 Stack trace: #0 C:\xampp\htdocs\Upload-Image\upload.php(33): PDO->exec('INSERT INTO tbl...') #1 {main} thrown in C:\xampp\htdocs\Upload-Image\upload.php on line 33" please tell what i can email is [email protected]

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.