Monday, March 27, 2017

Insert and display of a multiple mysql tables Data with php pdo and bootstrap.

Insert and display of a multiple mysql tables Data with php pdo and bootstrap.


In this php pdo tutorial, we are going to do simple method or technique to add or insertion in a multiple tables of mysql using php pdo.
First to start with this just you have to create database in phpmyadmin as before. Name it as "bramento".
And create mysql tables as categories and posts in bramento database.
And then add some columns on categories table as cat_id set as integer, and cat_title and set it as varchar and also in posts table post_id set as integer, post_title set as varchar ,image_title,post_image and set as varchar too.
  • Create db.php file now for php pdo connection of database

  1. <?php
  2.     $db_host = "localhost";
  3.     $db_name = "bramento";
  4.     $db_user = "root";
  5.     $db_pass = "";
  6.     
  7.     try{
  8.         
  9.         $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
  10.         $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.     }
  12.     catch(PDOException $e){
  13.         echo $e->getMessage();
  14.     }
  15.    ?>
  • And again now create index.php file where we create simple form with different columns inputs of two mysql database tables for insertion process,

  1. <div class="thumbnail">
  2. <h4 style="color:#F63"> Insert or add data in a multiple mysql tables with php pdo and bootstrap.</h4>
  3.  <form method="post" action="upload.php"  enctype="multipart/form-data">
  4. <table class="table1">
  5.     <tr>
  6.         <td><label style="color:#3a87ad; font-size:18px;"  > Title</label></td>
  7.         <td width="30"></td>
  8.         <td><input type="text" name="post_title" placeholder="title"  class="form-control" required /></td>
  9.     </tr>
  10.     <tr>
  11.         <td><label style="color:#3a87ad; font-size:18px;"  > Category</label></td>
  12.         <td width="30"></td>
  13.         <td><input type="text" name="cat_title" placeholder="categories"  class="form-control" required /></td>
  14.     </tr>
  15.     <tr>
  16.         <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
  17.         <td width="30"></td>
  18.         <td><input type="text" name="image_title" placeholder="image_title"  class="form-control" required /></td>
  19.     </tr>
  20.     <tr>
  21.         <td><label style="color:#3a87ad; font-size:18px;">Select  Image</label></td>
  22.         <td width="30"></td>
  23.         <td><input type="file" name="image"></td><!-- this file is going to save on uploads folder. create it if you have not created -->
  24.     </tr>        
  25. </table> 
  26. <button type="submit" name="Submit" class="btn btn-success">Submit</button>   
  27. </form>
  28.      </div>
  29.       <table class="table table-striped">
  30.     <thead>
  31.       <tr>
  32.         <th>Title</th>
  33.         <th>Image Title</th>
  34.         <th>Categories Title</th>
  35.         <th>Images</th>
  36.          <th>Delete</th>
  37.          <th>Update</th>
  38.       </tr>
  39.     </thead>  
  40.         <?php
  41.         include('db.php');
  42.         //query for selecting database table
  43.         
  44.        $results = $db_con->prepare("select c.* , p.* from categories c, posts p where c.cat_id= p.cat_id LIMIT 0 , 10");
  45. $results->execute();
  46.         // this while statement displays rows of database table
  47.         while($row=$results->fetch(PDO::FETCH_ASSOC))
  48.         {
  49.             extract($row);
  50.             ?>   
  51.           
  52.             <tbody>
  53.            <tr>
  54.         <td><?php echo $row['post_title']; ?></td>
  55.          <td><?php echo $row['image_title']; ?></td>
  56.          <td><?php echo $row['cat_title']; ?></td>
  57.          <td><img src="uploads/<?php echo $row['post_image']; ?>" class="img-rounded" alt="image" style="width:60px" height="60px;">
  58.             </td>
  59.            <td> <a href="editform.php?id=<?php echo $row['post_id'];?>" class="btn btn-warning">Update</a></td>
  60.            <td><a href="delete.php?post_id=<?php echo $post_id; ?>" class="btn btn-danger">Delete</a> </td>
  61.            
  62.             </tr>
  63.             </tbody>
  64.             <?php
  65.         }
  66.         ?>   
  67.         </table>
  68.         <br />
  • And At last create upload.php file where above form gonna submited here and process for inserting and uploading or inserting data on two tables posts and categories  

Here we  have inserted double query " INSERT INTO posts(image_title,post_image,post_title) VALUES( :image_title, :post_image, :post_title)" and
"INSERT INTO categories(cat_title) VALUES( :cat_title)" for insertion on posts and categories tables.
  1. <?php
  2.      include('db.php');
  3.     if(isset($_POST['Submit']))
  4.     {
  5.         
  6.         $post_image=$_FILES["image"]["name"];
  7. $post_title=$_POST['post_title'];
  8. $image_title=$_POST['image_title'];
  9. $cat_title=$_POST['cat_title'];
  10. }
  11.     
  12.         try{
  13.              move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
  14.             $stmt = $db_con->prepare("INSERT INTO posts(image_title,post_image,post_title) VALUES( :image_title, :post_image, :post_title)");
  15.             $stmt->bindParam(":post_title", $post_title);
  16.             $stmt->bindParam(":image_title", $image_title);
  17.             $stmt->bindParam(":post_image", $post_image);
  18.             $stmt1 = $db_con->prepare("INSERT INTO categories(cat_title) VALUES( :cat_title)");
  19.             $stmt1->bindParam(":cat_title", $cat_title); 
  20.             if($stmt->execute() AND $stmt1->execute() )
  21.             {
  22.                 echo "<script>alert('Successfully Added..')</script>";    
  23.         echo "<script>window.open('index.php','_self')</script>";
  24.             }
  25.             else{
  26.                 echo "Query Problem";
  27.             }    
  28.         }
  29.         catch(PDOException $e){
  30.             echo $e->getMessage();
  31.         }    
  32. ?>

As this way we finished to inserting and displaying mysql data on multiple mysql tables.

No comments:

Post a Comment