Skip to main content

PHP Insert Image In MySQL

Screenshot
Body

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` (<br />
  2. `tbl_image_id` INT(11) NOT NULL,<br />
  3. `first_name` VARCHAR(100) NOT NULL,<br />
  4. `last_name` VARCHAR(100) NOT NULL,<br />
  5. `image_location` VARCHAR(100) NOT NULL<br />
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Creating the Form Field - Modal Form

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

Creating Database Connection

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

Creating Insert Statement

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

Source Code for displaying data

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

Output:

Adding new user.

New UserAfter 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.

Comments

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

i downloaded the soure code.in 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 do......my email is [email protected]

Add new comment