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
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
- <title>Bootstrap 101 Template</title>
- <!-- Bootstrap -->
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
- </head>
- <body>
- <h1>multi insert select values</h1>
- <div class="col-md-6">
- <form method="post" action="add.php">
- <select name="multi[]" class="form-control" multiple="multiple">
- <option value="1">red</option>
- <option value="2">green</option>
- <option value="3">white</option>
- <option value="4">black</option>
- <option value="5">golden</option>
- <option value="6">yellow</option>
- </select><br>
- <input type="submit" name="submit" value="Add">
- </form>
- </div>
- <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
- <!-- Include all compiled plugins (below), or include individual files as needed -->
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
- </body>
- </html>
add.php
- <?php
- $db_host = "localhost";
- $db_name = "hey";
- $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();
- }
- try{
- if(isset($_POST['submit'])){
- $multi=$_POST['multi'];
- foreach ($multi as $multir) {
- $stmt=$db_con->prepare("insert into multi(multi) values(:multi)");
- $stmt->bindParam(":multi", $multir);
- $stmt->execute();
- # code...
- }
- }
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- ?>
No comments:
Post a Comment