PHP - Remove Image Data in MySQLi By Dragging

In this tutorial we will create Remove Image Data in MySQLi By Dragging using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_remove, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_remove");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Remove Image Data in MySQLi By Dragging</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="upload.php" enctype="multipart/form-data">
  18. <div class="form-inline">
  19. <input type="file" class="form-control" name="file" required="required"/>
  20. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  21. </div>
  22. </form>
  23. <br />
  24. <div class="col-md-8" style="padding:10px;">
  25. <?php
  26. require 'conn.php';
  27.  
  28. $query = mysqli_query($conn, "SELECT * FROM `image`") or die(mysqli_error());
  29. while($fetch = mysqli_fetch_array($query)){
  30. ?>
  31. <img src="<?php echo $fetch['location']?>" id="<?php echo $fetch['image_id']?>" height="150" width="150" style="float:left; margin:10px;" draggable="true" ondragstart="drag(event)"/>
  32. <?php
  33. }
  34. ?>
  35. </div>
  36. <div class="col-md-4" ondrop="drop(event)" ondragover="dragOver(event)">
  37. <img src="images/trash.png" height="200"/>
  38. </div>
  39. </div>
  40. <script src="js/script.js"></script>
  41. </body>
  42. </html>

Creating PHP Query

This code contains the php query of the application. This code will upload the image to the database server. To do that just copy and write this block of codes inside the text editor, then save it as upload.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['upload'])){
  5. $image_name = $_FILES['file']['name'];
  6. $image_temp = $_FILES['file']['tmp_name'];
  7. $allowed_ext = array("jpeg", "jpg", "gif", "png");
  8. $exp = explode(".", $image_name);
  9. $ext = end($exp);
  10. $name = date("Y-m-d h-i-s").".".$ext;
  11. $path = "uploads/".$name;
  12. if(in_array($ext, $allowed_ext)){
  13. if(move_uploaded_file($image_temp, $path)){
  14. mysqli_query($conn, "INSERT INTO `image` VALUES('', '$name', '$path')") or die(mysqli_error());
  15. header("location: index.php");
  16. }
  17. }else{
  18. echo "<script>alert('Invalid image format')</script>";
  19. }
  20. }
  21. ?>

Creating the Main Function

This code contains the main function of the application. This code remove the image data in the database server by dragging the image object to the target drop zone. To make this just follow and write the below inside the text editor, then save it as shown below. remove.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_REQUEST['image_id'])){
  5. $image = $_REQUEST['image_id'];
  6. $query = mysqli_query($conn, "SELECT * FROM `image` WHERE `image_id` = '$image'") or die(mysqli_error());
  7. $fetch = mysqli_fetch_array($query);
  8. if(unlink("uploads/".$fetch['image_name'])){
  9. mysqli_query($conn, "DELETE FROM `image` WHERE `image_id` = '$image'") or die(mysqli_error());
  10. header('location: index.php');
  11. }
  12. }
  13. ?>
script.js Note: Make sure you save this file inside the js directory to make the script works.
  1. function dragOver(e){
  2. e.preventDefault();
  3. }
  4.  
  5. function drop(e){
  6. e.preventDefault();
  7. var data = e.dataTransfer.getData("data");
  8.  
  9. window.location = "remove.php?image_id="+data;
  10. }
  11.  
  12. function drag(e){
  13. e.dataTransfer.setData("data", e.target.id);
  14. }
There you have it we successfully created Remove Image Data in MySQLi By Dragging using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment