Monday, March 27, 2017

Crud in php pdo.


This is a simple crud for inserting ,updating and deleting mysql data using php pdo
To do this first just create database tablecrud  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 db.php file for database connection as

  1. <?php
  2. //this scripts connect to your database
  3.     $db_host = "localhost";
  4.     $db_name = "tablecrud";
  5.     $db_user = "root";
  6.     $db_pass = "";
  7.     try{
  8.         $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
  9.         $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.     }
  11.     catch(PDOException $e){
  12.         echo $e->getMessage();
  13.     }
  14. ?>

Now, create index.php file write some php pdo scripts as this and also create form as this below of index.php file 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title> How to delete Images of a folder permanently.</title>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  7.  <script src="../js/jquery.min.js"></script>
  8.   <script src="../bootstrap/bootstrap.min.js"></script>      
  9. </head>
  10. <body>
  11.     <div class="container">
  12.    
  13.   <div class="thumbnail">
  14.   <h4 style="color:#F06"> Add, Edit ,Delete and Display using php pdo and bootstrap.</h4>
  15.  <form method="post" action="upload.php"  enctype="multipart/form-data">
  16. <table class="table1">
  17.     <tr>
  18.         <td><label style="color:#3a87ad; font-size:18px;"  > Title</label></td>
  19.         <td width="30"></td>
  20.         <td><input type="text" name="post_title" placeholder="title"  class="form-control" required /></td>
  21.     </tr>
  22.     <tr>
  23.         <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
  24.         <td width="30"></td>
  25.         <td><input type="text" name="image_title" placeholder="image_title"  class="form-control" required /></td>
  26.     </tr>
  27.     <tr>
  28.         <td><label style="color:#3a87ad; font-size:18px;">Select  Image</label></td>
  29.         <td width="30"></td>
  30.         <td><input type="file" name="image"></td>
  31.     </tr>    
  32.      
  33. </table> 
  34. <button type="submit" name="Submit" class="btn btn-success">Submit</button>   
  35. </form>
  36. </div>
  37.          
  38.       <table class="table table-striped">
  39.     <thead>
  40.       <tr>
  41.         <th>Title</th>
  42.         <th>Image Title</th>
  43.         <th>Images</th>
  44.          <th>Delete</th>
  45.          <th>Update</th>
  46.       </tr>
  47.     </thead>  
  48.         <?php
  49.         include('db.php');
  50.         //query for selecting database table
  51.        $results = $db_con->prepare("SELECT * FROM posts ORDER BY post_id");
  52. $results->execute();
  53.         // this while statement displays rows of database table
  54.         while($row=$results->fetch(PDO::FETCH_ASSOC))
  55.         {
  56.             extract($row);
  57.             ?>   
  58.           
  59.             <tbody>
  60.            <tr>
  61.         <td><?php echo $row['post_title']; ?></td>
  62.          <td><?php echo $row['image_title']; ?></td>
  63.          
  64.          <td><img src="uploads/<?php echo $row['post_image']; ?>" class="img-rounded" alt="image" style="width:60px" height="60px;">
  65.             </td>
  66.            <td> <a href="editform.php?id=<?php echo $row['post_id'];?>" class="btn btn-info">Update</a></td>
  67.            <td><a href="delete.php?post_id=<?php echo $post_id; ?>" class="btn btn-danger">Delete</a> </td>
  68.            
  69.             </tr>
  70.             </tbody>
  71.             <?php
  72.         }
  73.         ?>       
  74.         </table>  
  75.        </div>
  76. </body>
  77. </html>

No comments:

Post a Comment