| | |
How to update database from checkboxes?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Solved Threads: 0
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
------------------
Thanks,
Debbie
------------------
php Syntax (Toggle Plain Text)
<?php /* insert code to connect to your database here */ require_once ('#####'); // Enter info to connect to the db. /* get the checkbox labels */ $skills = get_checkbox_labels("const_skills"); /* create the html code for a formatted set of checkboxes */ $html_skills = make_checkbox_html($skills, 3, 400, "skills[]"); ?> <html> <body> <br> <form name="skills" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Check off your web development skills: <? echo "$html_skills"; ?> <br> <input type="submit" value="Submit"> </form> </body> </html> <?php function get_checkbox_labels($table_name) { /* make an array */ $arr = array(); /* construct the query */ $query = "SELECT * FROM $table_name"; /* execute the query */ $qid = mysql_query($query); /* each row in the result set will be packaged as an object and put in an array */ while($row= mysql_fetch_object($qid)) { array_push($arr, $row); } return $arr; } /* Prints a nicely formatted table of checkbox choices. $arr is an array of objects that contain the choices $num is the number of elements wide we display in the table $width is the value of the width parameter to the table tag $name is the name of the checkbox array $checked is an array of element names that should be checked */ function make_checkbox_html($arr, $num, $width, $name) { /* create string to hold out html */ $str = ""; /* make it */ $str .= "<table width=\"$width\" border=\"0\">\n"; $str .= "<tr>\n"; /* determine if we will have to close add a closing tr tag at the end of our table */ if (count($arr) % $num != 0) { $closingTR = true; } $i = 1; if (isset($checked)) { /* if we passed in an array of the checkboxes we want to be displayed as checked */ foreach ($arr as $ele) { $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\""; foreach ($checked as $entry) { if ($entry == $ele->value) { $str .= "checked"; continue; } } $str .= ">"; $str .= "$ele->value"; if ($i % $num == 0) { $str .= "</tr>\n<tr>"; } else { $str .= "</td>\n"; } $i++; } } else { /* we just want to print the checkboxes. none will have checks */ foreach ($arr as $ele) { $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\">"; $str .= "$ele->value"; if ($i % $num == 0) { $str .= "</tr>\n<tr>"; } else { $str .= "</td>\n"; } $i++; } } /* tack on a closing tr tag if necessary */ if ($closingTR == true) { $str .= "</tr></table>\n"; } else { $str .= "</table>\n"; } return $str; } ?> <?php // $q = "INSERT INTO lookup_skills (uid, skill_id) VALUES"; // foreach ($arr as $check) { // $q .= " ( $uid , $check )" . ","; // } // $result = @mysql_query ($q); // Run the query. /* the function we call to insert. the $skills argument is the skills array that is sent to the script when the user hits the submit button */ function insert_skills($uid, $skills) { /* first, we'll delete any entries this user already has in the table */ purge_lookup("lookup_skills", $uid); /* now create the sql insert query */ $query = create_checkbox_query($skills, "lookup_skills", $uid); /* execute the query */ mysql_query($query); } /* helper function for insert_skills(). removes all rows in $table with $uid */ function purge_lookup($table, $uid) { $q = "DELETE FROM $table, WHERE uid = '$uid'"; mysql_query($q); } /* helper function for insert_skills(). generates the sctual SQL query */ function create_checkbox_query($arr, $table, $uid) { $q = "INSERT INTO $table (uid, skill_id) VALUES"; foreach ($arr as $check) { $q .= " ( $uid , $check )" . ","; mysql_query ($q); } /* remove the last comma and return */ return substr($q, 0, -1); } ?>
Last edited by peter_budo; Mar 23rd, 2008 at 5:57 am. Reason: Keep It Organized - please use [code] tags
![]() |
Similar Threads
- Checkbox Arrays ARGH!! (PHP)
- inserting checkbox values in mysql +PHP (PHP)
- Database design - ASP.NET MsAccess (ASP.NET)
- datagrid, innertext, javascript, database update (ASP.NET)
- Update Scoreboard - ASP (ASP)
- How to pre-select checkboxes in form? (PHP)
- antispylab on dad's computer :( (Viruses, Spyware and other Nasties)
- counting checkboxes (ASP.NET)
- Problem Unsetting Variables? (PHP)
Other Threads in the PHP Forum
- Previous Thread: File Permissions and Register_globle
- Next Thread: dynamic html
| Thread Tools | Search this Thread |
5.2.10 action address ajax apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display domain dynamic echo email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image include insert integration ip java javascript joomla limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop paypal pdf php popup problem query radio random record recursion regex remote script search security server sessions sms soap sockets source space sql syntax system table tutorial update upload url validator variable video web xml youtube






