PHP - Simple Insert Multiple Entries

In this tutorial we will create a Simple Insert Multiple Entries using PHP. This code will allow the user to insert multiples form data in just one click. The code use javascript onclick() function to create a new element in the form, then when submit the php script will break down the form array by using foreach in order to insert the in the MySQLi database one by one. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interepret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_entry, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=mysqli_connect('localhost', 'root', '', 'db_entry');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="comtainer-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Insert Multiple Entries</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" onclick="addEntry();"><span class="glyphicon glyphicon-plus"></span> Add entry</button>
  18. <br />
  19. <br />
  20. <br />
  21. <div class="col-md-5">
  22. <form method="POST" action="insert.php">
  23. <div id="form-entry">
  24. <div class="form-group">
  25. <input type="text" name="entry[]" placeholder="Enter here..." class="form-control" required="required"/>
  26. </div>
  27. </div>
  28. <button class="btn btn-primary" name="insert"><span class="glyphicon glyphicon-save"></span> Insert</button>
  29. </form>
  30. </div>
  31. <div class="col-md-2"></div>
  32. <div class="col-md-4">
  33. <table class="table table-bordered">
  34. <thead>
  35. <tr>
  36. <th>Name</th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <?php
  41. require'conn.php';
  42.  
  43. $query=mysqli_query($conn, 'SELECT * FROM `member`') or die(mysqli_error());
  44. while($fetch=mysqli_fetch_array($query)){
  45. ?>
  46. <tr>
  47. <td><?php echo $fetch['name']?></td>
  48. </tr>
  49. <?php
  50. }
  51. ?>
  52. </tbody>
  53. </table>
  54. </div>
  55. </div>
  56. <script src="js/script.js"></script>
  57. </body>
  58. </html>

Creating the Main Function

This code contains the main function of the application. This code will insert multiple data when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. insert.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['insert'])){
  5. $entry = $_POST['entry'];
  6.  
  7. foreach($entry as $value){
  8. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$value')") or die(mysqli_error());
  9. }
  10.  
  11. header("location: index.php");
  12. }
  13. ?>
script.js Note: To make this code work make sure you save this file inside the js directory.
  1. function addEntry(){
  2. var entry="<input type='text' name='entry[]' placeholder='Enter here...' class='form-control' required='required'/>";
  3. var element=document.createElement("div");
  4. element.setAttribute('class', 'form-group');
  5. element.innerHTML=entry;
  6. document.getElementById('form-entry').appendChild(element);
  7. }
There you have it we successfully created Simple Insert Multiple Entries using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment