In this section, we are going to delete Images of a folder permanently and also data and Insert or Add Data and Display in a bootstrap table along with uploading and displaying images with php pdo..
This is just a simple techniques to delete Images of a folder permanently and also data and Insert or Add Data and Display in a bootstrap table along with uploading and displaying images with php pdo... This is soo easy to do it.
To do this first just create database dataadd and then create table as posts and then create columns post_id ,post_title,image_title and post_image as integer,varchar,varchar and varchar respectively..
First create uploads folder for images saving on it and displaying on your browser through server.
Now Lets Begin, Create index.php file.
and create form on index.php as like this
- <form method="post" action="upload.php" enctype="multipart/form-data">
- <table class="table1">
- <tr>
- <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
- <td width="30"></td>
- <td><input type="text" name="post_title" placeholder="image_title" required /></td>
- </tr>
- <tr>
- <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
- <td width="30"></td>
- <td><input type="text" name="image_title" placeholder="image_title" required /></td>
- </tr>
- <tr>
- <td><label style="color:#3a87ad; font-size:18px;">Select Image</label></td>
- <td width="30"></td>
- <td><input type="file" name="image"></td>
- </tr>
- </table>
- <button type="submit" name="Submit" class="btn btn-success">Submit</button>
- </form>
Now lets jump on php pdo scripts to proceed for database connection first.
create new php file again and save it as db.php and paste these scripts on this file.
- <?php
- //this scripts connect to your database
- $db_host = "localhost";
- $db_name = "dataadd";
- $db_user = "root";
- $db_pass = "";
- try{
- $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
- $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- ?>
And Again lets create upload.php where our form gonna submited on this page and proceed for uploading and adding texts on uploads folder and mysql table that we have created.
Add some scripts for submiting process of our form now on upload.php
- <?php
- include('db.php');
- if(isset($_POST['Submit']))
- {
- $post_image=$_FILES["image"]["name"];
- $post_title=$_POST['post_title'];
- $image_title=$_POST['image_title'];
- }
- try{
- move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
- //this query is for insertion process
- $stmt = $db_con->prepare("INSERT INTO posts(image_title,post_image,post_title) VALUES( :image_title, :post_image, :post_title)");
- $stmt->bindParam(":post_title", $post_title);
- $stmt->bindParam(":image_title", $image_title);
- $stmt->bindParam(":post_image", $post_image);
- if($stmt->execute())
- {
- echo "<script>alert('Successfully Added..')</script>";
- echo "<script>window.open('index.php','_self')</script>";
- }
- else{
- echo "Query Problem";
- }
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- ?>
And Now create delete.php file and write some scrpts which delete our mysql table datas and also delete images of our folder uploads permanently.
<?php
require_once('db.php');
require_once('db.php');
$get_id=$_GET['post_id'];
//query for selecting database table
$stmt = $db_con->prepare("SELECT * FROM posts WHERE post_id = '$get_id'");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<?php unlink("uploads/".$row['post_image']); ?> // this unlink function delete uploaded images permanently
<?php
}
?>
<?php
$get_id=$_GET['post_id'];
// sql to delete a record
$stmt = $db_con->prepare("SELECT * FROM posts WHERE post_id = '$get_id'");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<?php unlink("uploads/".$row['post_image']); ?> // this unlink function delete uploaded images permanently
<?php
}
?>
<?php
$get_id=$_GET['post_id'];
// sql to delete a record
$sql = "Delete from posts where post_id = '$get_id'";
// use exec() because no results are returned
$db_con->exec($sql);
// use exec() because no results are returned
$db_con->exec($sql);
header("Location: index.php");
?>
?>
And last to display uploaded images from the uploads folder and text or image title post title from the database table posts just copy and paste thes scripts on your index.php file .
- <table class="table table-striped">
- <thead>
- <tr>
- <th>Title</th>
- <th>Image Title</th>
- <th>Images</th>
- <th>Delete</th>
- </tr>
- </thead>
- <?php
- include('db.php');
- //query for selecting database table
- $results = $db_con->prepare("SELECT * FROM posts ORDER BY post_id");
- $results->execute();
- // this while statement displays rows of database table
- while($row=$results->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <tbody>
- <tr>
- <td><?php echo $row['post_title']; ?></td>
- <td><?php echo $row['image_title']; ?></td>
- <td><img src="uploads/<?php echo $row['post_image']; ?>" class="img-rounded" alt="image" style="width:60px" height="60px;">
- </td>
- <td><a href="delete.php?post_id=<?php echo $post_id; ?>" class="btn btn-danger">Delete</a> </td>
- </tr>
- </tbody>
- <?php
- }
- ?>
- </table>
full index.php file
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title> How to delete Images of a folder permanently.</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
- <script src="../js/jquery.min.js"></script>
- <script src="../bootstrap/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <div class="thumbnail">
- <h4 style="color:#F06"> How to delete Images of a folder permanently and Insert or Add Data and Display in a bootstrap table along with uploading and displaying images with php pdo.</h4>
- <form method="post" action="upload.php" enctype="multipart/form-data">
- <table class="table1">
- <tr>
- <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
- <td width="30"></td>
- <td><input type="text" name="post_title" placeholder="image_title" required /></td>
- </tr>
- <tr>
- <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
- <td width="30"></td>
- <td><input type="text" name="image_title" placeholder="image_title" required /></td>
- </tr>
- <tr>
- <td><label style="color:#3a87ad; font-size:18px;">Select Image</label></td>
- <td width="30"></td>
- <td><input type="file" name="image"></td>
- </tr>
- </table>
- <button type="submit" name="Submit" class="btn btn-success">Submit</button>
- </form>
- </div>
- <table class="table table-striped">
- <thead>
- <tr>
- <th>Title</th>
- <th>Image Title</th>
- <th>Images</th>
- <th>Delete</th>
- </tr>
- </thead>
- <?php
- include('db.php');
- //query for selecting database table
- $results = $db_con->prepare("SELECT * FROM posts ORDER BY post_id");
- $results->execute();
- // this while statement displays rows of database table
- while($row=$results->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <tbody>
- <tr>
- <td><?php echo $row['post_title']; ?></td>
- <td><?php echo $row['image_title']; ?></td>
- <td><img src="uploads/<?php echo $row['post_image']; ?>" class="img-rounded" alt="image" style="width:60px" height="60px;">
- </td>
- <td><a href="delete.php?post_id=<?php echo $post_id; ?>" class="btn btn-danger">Delete</a> </td>
- </tr>
- </tbody>
- <?php
- }
- ?>
- </table>
- </div>
- </body>
- </html>
full upload.php file
- <?php
- include('db.php');
- if(isset($_POST['Submit']))
- {
- $post_image=$_FILES["image"]["name"];
- $post_title=$_POST['post_title'];
- $image_title=$_POST['image_title'];
- }
- try{
- move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
- $stmt = $db_con->prepare("INSERT INTO posts(image_title,post_image,post_title) VALUES( :image_title, :post_image, :post_title)");
- $stmt->bindParam(":post_title", $post_title);
- $stmt->bindParam(":image_title", $image_title);
- $stmt->bindParam(":post_image", $post_image);
- if($stmt->execute())
- {
- echo "<script>alert('Successfully Added..')</script>";
- echo "<script>window.open('index.php','_self')</script>";
- }
- else{
- echo "Query Problem";
- }
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- ?>
Full db.php file
- <?php
- date_default_timezone_set("Asia/Kathmandu");
- $db_host = "localhost";
- $db_name = "dataadd";
- $db_user = "root";
- $db_pass = "";
- try{
- $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
- $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- ?>
full delete.php file
- <?php
- require_once('db.php');
- $get_id=$_GET['post_id'];
- $stmt = $db_con->prepare("SELECT * FROM posts WHERE post_id = '$get_id'");
- $stmt->execute();
- while($row=$stmt->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <?php unlink("uploads/".$row['post_image']); ?>
- <?php
- }
- ?>
- <?php
- $get_id=$_GET['post_id'];
- // sql to delete a record
- $sql = "Delete from posts where post_id = '$get_id'";
- // use exec() because no results are returned
- $db_con->exec($sql);
- header("Location: index.php");
- ?>
No comments:
Post a Comment