PHP - Simple Insert Multiple Checkbox Data

Submitted by: 
Language: 
Visitors have accessed this post 258 times.

In this tutorial we will create a Simple Insert Multiple Checkbox Data using PHP. This code will allow the user to insert multiples checkbox data to the database server in one click. The code use MySQLi INSERT query to store a new entry in the MySQLi database, then when submit the php script will compress the checkbox array by using implode in order to submit the data to the database as one entry. 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_insert_multiple, 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_insert_multiple');
  3.  
  4. if(!$conn){
  5. die("Error: Can't 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="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Insert Multiple Checkbox Data</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" action="save.php">
  20. <div class="form-group">
  21. <label>Firstname</label>
  22. <input type="text" name="firstname" class="form-control" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Lastname</label>
  26. <input type="text" name="lastname" class="form-control" required="required"/>
  27. </div>
  28. <label>Favorite Subjects:</label>
  29. <br />
  30. <input type="checkbox" name="subject[]" value="Math"/>Math
  31. <br />
  32. <input type="checkbox" name="subject[]" value="English"/>English
  33. <br />
  34. <input type="checkbox" name="subject[]" value="Science"/>Science
  35. <br />
  36. <input type="checkbox" name="subject[]" value="History"/>History
  37. <br /><br />
  38. <button name="save" class="btn btn-primary form-control">Save</button>
  39. </form>
  40. </div>
  41.  
  42. <div class="col-md-8">
  43. <table class="table table-bordered">
  44. <thead class="alert-info">
  45. <tr>
  46. <th>Firstname</th>
  47. <th>Lastname</th>
  48. <th>Subjects</th>
  49. </tr>
  50. </thead>
  51. <tbody>
  52. <?php
  53. require'conn.php';
  54. $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  55. while($fetch=mysqli_fetch_array($query)){
  56. ?>
  57. <tr>
  58. <td><?php echo $fetch['firstname']?></td>
  59. <td><?php echo $fetch['lastname']?></td>
  60. <td><?php echo $fetch['subject']?></td>
  61. </tr>
  62. <?php
  63. }
  64. ?>
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. </body>
  70. </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 save.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7.  
  8.  
  9. $subjects=implode(", ", $_POST['subject']);
  10.  
  11. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$subjects')") or die(mysqli_error());
  12.  
  13. header('location:index.php');
  14. }
  15.  
  16.  
  17. ?>

There you have it we successfully created Simple Insert Multiple Checkbox Data 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.