How to update database from checkboxes?

Reply

Join Date: Sep 2007
Posts: 4
Reputation: hsmom314 is an unknown quantity at this point 
Solved Threads: 0
hsmom314 hsmom314 is offline Offline
Newbie Poster

How to update database from checkboxes?

 
0
  #1
Mar 21st, 2008
I'm trying to update a mySQL database from the info contained in an html form with checkboxes using PHP. Here's an example of what I'm trying to do:
Stock number: 555 is available in red, white, blue, and green. My form will have a text box for the stock number and checkboxes for the colors. My database should show 4 rows with 2 columns: stock number 555 (in column 1) and red (in column 2); then 555 and white (in the next row); then 555 and blue; etc. The stock number should repeat in each row until all of the available colors are shown (only one color will show in the column in each row). Is this possible? How would I do this? I'm fairly new to PHP so please include as much detail as possible. If you can recommend any courses/books to learn PHP more indepth, it would be greatly appreciated.

Thanks,
Debbie
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: How to update database from checkboxes?

 
0
  #2
Mar 21st, 2008
Can you post your code ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 4
Reputation: hsmom314 is an unknown quantity at this point 
Solved Threads: 0
hsmom314 hsmom314 is offline Offline
Newbie Poster

Re: How to update database from checkboxes?

 
0
  #3
Mar 21st, 2008
I had found the code below when searching for a tutorial on checkboxes and I have tried adapting it to my situation but it does not update the database and I am at a loss as to why it doesn't update. The checkboxes display properly but when I select them and press submit, the database doesn't update.

Thanks,
Debbie

------------------

  1. <?php
  2.  
  3. /* insert code to connect to your database here */
  4. require_once ('#####'); // Enter info to connect to the db.
  5. /* get the checkbox labels */
  6. $skills = get_checkbox_labels("const_skills");
  7.  
  8. /* create the html code for a formatted set of
  9.   checkboxes */
  10. $html_skills = make_checkbox_html($skills, 3, 400, "skills[]");
  11.  
  12. ?>
  13.  
  14. <html>
  15. <body>
  16. <br>
  17. <form name="skills" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  18. Check off your web development skills:
  19. <? echo "$html_skills"; ?>
  20. <br>
  21. <input type="submit" value="Submit">
  22. </form>
  23. </body>
  24. </html>
  25. <?php
  26.  
  27. function get_checkbox_labels($table_name) {
  28.  
  29. /* make an array */
  30. $arr = array();
  31.  
  32. /* construct the query */
  33. $query = "SELECT * FROM $table_name";
  34.  
  35. /* execute the query */
  36. $qid = mysql_query($query);
  37.  
  38. /* each row in the result set will be packaged as
  39.   an object and put in an array */
  40. while($row= mysql_fetch_object($qid)) {
  41. array_push($arr, $row);
  42. }
  43.  
  44. return $arr;
  45. }
  46.  
  47. /* Prints a nicely formatted table of checkbox choices.
  48.  
  49.   $arr is an array of objects that contain the choices
  50.   $num is the number of elements wide we display in the table
  51.   $width is the value of the width parameter to the table tag
  52.   $name is the name of the checkbox array
  53.   $checked is an array of element names that should be checked
  54. */
  55.  
  56.  
  57. function make_checkbox_html($arr, $num, $width, $name) {
  58.  
  59. /* create string to hold out html */
  60. $str = "";
  61.  
  62. /* make it */
  63. $str .= "<table width=\"$width\" border=\"0\">\n";
  64. $str .= "<tr>\n";
  65.  
  66. /* determine if we will have to close add
  67.   a closing tr tag at the end of our table */
  68. if (count($arr) % $num != 0) {
  69. $closingTR = true;
  70. }
  71.  
  72. $i = 1;
  73. if (isset($checked)) {
  74. /* if we passed in an array of the checkboxes we want
  75.   to be displayed as checked */
  76. foreach ($arr as $ele) {
  77. $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\"";
  78. foreach ($checked as $entry) {
  79. if ($entry == $ele->value) {
  80. $str .= "checked";
  81. continue;
  82. }
  83. }
  84. $str .= ">";
  85. $str .= "$ele->value";
  86.  
  87. if ($i % $num == 0) {
  88. $str .= "</tr>\n<tr>";
  89. } else {
  90. $str .= "</td>\n";
  91. }
  92. $i++;
  93. }
  94.  
  95. } else {
  96. /* we just want to print the checkboxes. none will have checks */
  97. foreach ($arr as $ele) {
  98. $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\">";
  99. $str .= "$ele->value";
  100.  
  101. if ($i % $num == 0) {
  102. $str .= "</tr>\n<tr>";
  103. } else {
  104. $str .= "</td>\n";
  105. }
  106. $i++;
  107. }
  108.  
  109. }
  110.  
  111. /* tack on a closing tr tag if necessary */
  112. if ($closingTR == true) {
  113. $str .= "</tr></table>\n";
  114. } else {
  115. $str .= "</table>\n";
  116. }
  117.  
  118. return $str;
  119. }
  120.  
  121.  
  122. ?>
  123.  
  124. <?php
  125.  
  126. // $q = "INSERT INTO lookup_skills (uid, skill_id) VALUES";
  127. // foreach ($arr as $check) {
  128. // $q .= " ( $uid , $check )" . ",";
  129. // }
  130. // $result = @mysql_query ($q); // Run the query.
  131.  
  132.  
  133. /* the function we call to insert.
  134.   the $skills argument is the skills array that
  135.   is sent to the script when the user hits the submit button
  136. */
  137. function insert_skills($uid, $skills) {
  138.  
  139. /* first, we'll delete any entries this user already has
  140.   in the table */
  141. purge_lookup("lookup_skills", $uid);
  142.  
  143. /* now create the sql insert query */
  144. $query = create_checkbox_query($skills, "lookup_skills", $uid);
  145.  
  146. /* execute the query */
  147. mysql_query($query);
  148. }
  149.  
  150. /* helper function for insert_skills().
  151.   removes all rows in $table with $uid */
  152. function purge_lookup($table, $uid) {
  153. $q = "DELETE FROM $table, WHERE uid = '$uid'";
  154. mysql_query($q);
  155. }
  156.  
  157. /* helper function for insert_skills().
  158.   generates the sctual SQL query */
  159. function create_checkbox_query($arr, $table, $uid) {
  160. $q = "INSERT INTO $table (uid, skill_id) VALUES";
  161.  
  162. foreach ($arr as $check) {
  163. $q .= " ( $uid , $check )" . ",";
  164. mysql_query ($q);
  165. }
  166.  
  167. /* remove the last comma and return */
  168. return substr($q, 0, -1);
  169. }
  170.  
  171. ?>
Last edited by peter_budo; Mar 23rd, 2008 at 5:57 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: How to update database from checkboxes?

 
0
  #4
Mar 22nd, 2008
Umm.. Update or insert ? Print out your query to see if all the required values are passed. Print_r($checkbox_name) will print the values of those checkbox which were selected.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC