By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

how to save values of checkbox in mysql using php

P: 1
I am working on a simple attendance/registration project to be used in an academic triathlon. In the the registration form, I have the fields for Name (firsname,middle,lastname), School, Contact details (Contact number and Email), Visitor Type (Participant,Coach,Visitor) and Event. For the Name , school,and contact details I used the textboxes while for the Visitor Type I used a drop down and for the event I used checkboxes.

Now I have no problem saving the all of the values in the form except for the checkboxes. When tick one of the checkboxes and clicked submit the data given to the database in the Event field is "Array".

here is the code I used:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require("include/include.php");
  4.  
  5. if (!isset($_SESSION['username']))
  6. {
  7.  
  8. header("Location: index.php");
  9.  
  10. }
  11.  
  12. $type = array ("Participant" , "Coach" , "Visitor");    
  13.  
  14. beginHTML();
  15. pageTitle("Registration :: LPL Academic Triathlon");
  16. addCSS("css/style.css");
  17. beginBody();
  18.  
  19. echo "<h3>Register</h3>";
  20.  
  21. if (isset($_POST['register']))
  22.  {
  23. $firstname = addslashes($_POST['firstname']);
  24. $middle = addslashes($_POST['middle']);
  25. $lastname = addslashes($_POST['lastname']);
  26. $school = addslashes($_POST['school']);
  27. $number = addslashes($_POST['number']);
  28. $email = addslashes($_POST['email']);
  29. $type = $_POST['type'];
  30. $event = $_POST['event'];
  31.  
  32. mysql_query("INSERT INTO member (Firstname,Middle,Lastname,SchoolCode,Number,Email,Type,Event) VALUES ('$firstname','$middle','$lastname','$school','$number','$email','$type','$event')");
  33.                                                         echo "<p>User Successfully Added!</p>";
  34. echo "<p><a href=\"register.php\">[ Back ]</a></p>";
  35. else 
  36. {
  37. ?>
  38.  
  39. <form action="" method="post">
  40. <table>
  41. <tr>
  42. <td>Firstname: </td>
  43. <td><input name="firstname" type="text" value="" /></td>
  44. </tr>
  45. <tr>
  46. <td>Middle Initial: </td>
  47. <td><input name="middle" type="text" value="" size="1" /></td>
  48. </tr>
  49. <tr>
  50. <td>Lastname</td>
  51. <td><input name="lastname" type="text" value="" /></td>
  52. </tr>
  53. <tr>
  54. <td>School</td>
  55. <td><input name="school" type="text" value="" size="35" /></td>
  56. </tr>
  57. <tr>
  58. <td>Contact Details</td>
  59. </tr>
  60. <tr>
  61. <td>Contact Number: </td>
  62. <td><input name="number" type="text" value="" /></td>
  63. </tr>
  64. <tr>
  65. <td>Email: </td>
  66. <td><input name="email" type="text" value="" /></td>
  67. </tr>
  68. <tr>
  69. <td>Type</td>
  70. <td>
  71. <select name = "type">
  72. <option value = "None"> --- Choose an Option --- </option>
  73.  
  74. <?php
  75. foreach ($type as $typ)
  76. {
  77. echo "<option value = '$typ'>$typ</option>";
  78. }            
  79. switch ($type)
  80. {
  81.                             case "Participant":                            break;
  82.                             case "Coach":
  83. break;
  84.                             case "Visitor":
  85. break;
  86. }
  87. ?>
  88.  
  89. </select> <br />
  90. </td>
  91. </tr>
  92. <tr>
  93. <td>Event</td>
  94. <td>
  95. <label><input type = "checkbox" name = "event[]" value = "Reality Quiz Show" /> Reality Quiz Show</label><br />
  96. <label><input type = "checkbox" name = "event[]" value = "Math Olympics" /> Math Olympics</label><br />
  97. <label><input type = "checkbox" name = "event[]" value = "Elocution" /> Elocution</label><br />
  98. </td>    
  99. </tr>
  100. </table>
  101. <p><input name="register" type="submit" value="Register" /></p>
  102. </form>
  103.  
  104. <?php
  105. }
  106. endBody();
  107. endHTML();
  108. ?>
  109.  
  110.  

When I remove the []in the name=event[], then ticked all of the checboxes the value of the last checkbox is registered in the database.

I tried using the serialize and unserialize functions but I dont understand how it works.



Hoping for an immediate reply from you guys. Thanks... :)
Jan 25 '12 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
When tick one of the checkboxes and clicked submit the data given to the database in the Event field is "Array".
"Array" is the string representation of any array. cf. echo array();


When I remove the []in the name=event[], then ticked all of the checboxes the value of the last checkbox is registered in the database.
due to overwriting in the URL: ?name=1&name=2 => $_GET['name'] => 2


your main problem is that you have multiple form values (name="event[]") under the same name. the fault in your logic is that you want to save multiple data items in a single database field. you could of course use serialisation to make these value into one but that is very bad practice. What you would need to do is find out how to solve that problem in general.
an option would be Database normalisation (I think Atli has made an article about that). esp. type & event do not belong in a user data table (a user usually can't change his name, but may have several events. a classical case where you would have a second table where you link the events to the users (or vice versa))
Jan 25 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.