Monday, March 27, 2017

How to insert multiple rows or selectlist values in a mysql table with php pdo

This tutorial is about inserting multiple rows or selectlist values in a mysql table with php pdo.
I have used bootstrap framework for css content. Go to the getbootstrap.com for it.
And create simple layout with bootstrap and create form and inside the form create select list.
And after create database "hey" or name it  as your wish and also create mysql table and inside that table again create coumns as id as integer  and insert values upto 254 autoincrement , set it as primarykey and create multi columns and insert values upto 254 set it as varchar on phpmyadmin
create some folder on c/xampp/htdocs of your computer and create index.php file and also create add.php file. The index.php file contain bootstrap layout and form with select list where as add.php file is for form processing for adding data.
index.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">
  7.     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  8.     <title>Bootstrap 101 Template</title>
  9.     <!-- Bootstrap -->
  10.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  11.   </head>
  12.   <body>
  13.     <h1>multi insert select values</h1>
  14.     <div class="col-md-6">
  15.     <form method="post" action="add.php">
  16.       <select name="multi[]" class="form-control" multiple="multiple">
  17.         <option value="1">red</option>
  18.         <option value="2">green</option>
  19.         <option value="3">white</option>
  20.         <option value="4">black</option>
  21.         <option value="5">golden</option>
  22.         <option value="6">yellow</option>
  23.       </select><br>
  24.       <input type="submit" name="submit" value="Add">
  25.     </form>
  26.     </div>
  27.     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  28.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  29.     <!-- Include all compiled plugins (below), or include individual files as needed -->
  30.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  31.   </body>
  32. </html>
add.php
  1.  <?php
  2.      $db_host = "localhost";
  3.     $db_name = "hey";
  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.     try{
  16.     if(isset($_POST['submit'])){
  17.         $multi=$_POST['multi'];
  18.     foreach ($multi as $multir) {
  19.     
  20.     $stmt=$db_con->prepare("insert into multi(multi) values(:multi)");
  21.     $stmt->bindParam(":multi", $multir);
  22.     $stmt->execute();
  23.       # code...
  24.     }
  25.     }
  26.    }
  27.       catch(PDOException $e){
  28.       echo $e->getMessage();
  29.     
  30.   }
  31.    ?>

 Video here


No comments:

Post a Comment