How can I insert checkbox value using php and mysql into database?
-
I want to make a form where there are multiple checkboxes such as Multimedia, Bluetooth, Camera, Memory and so on. What should be write in php command? What should be write in mysql command? what should be write in database table command to store from the input form using POST method? And how the result can be published?
-
Answer:
**To allow multiple checkboxes to be checked, give each box the same name then connect to your database and run a check for each box by giving them a unique value before updating your database. Example : <?php //set up some variables $server = "//mysql server address" ; $user = "//database username" ; $password ="//database password" ; $table_name = "//database table name" ; $checkbox_name = "//checkbox name" ; $column_name = "//table column name" ; //connect to database $connect = mysql_connect("$server" , "$user" , "$password"); if (!$connect) { die('Could not connect: ' . mysql_error()); } //check for which boxes are selected then update database if(isset($_POST['$checkbox_name'])) { foreach($_POST['$checkbox_name'] as $value){ $insert=mysql_query("INSERT INTO $table_name($column_name') VALUES ('$value')"); } } ?> <html> <body> <form method="post" action="checkbox_form.php"> <input type="checkbox" name="<?php echo '$checkbox_name' ?>" value="Multimedia"> Multimedia /> <input type="checkbox" name="<?php echo '$checkbox_name' ?>" value="Bluetooth"> Bluetooth /> <input type="checkbox" name="<?php echo '$checkbox_name' ?>" value="Camera"> Camera /> <input type="checkbox" name="<?php echo '$checkbox_name' ?>" value="Memory"> Memory /> <input type="submit" name="submit" value="submit"> </form> </body> </html> **To publish the results, simply retrieve the $value of $column_name and echo the results in checkbox_form.php Example: (contents of checkbox_form.php) <?php //set up some variables $server = "//mysql server address" ; $user = "//database username" ; $password ="//database password" ; $table_name = "//database table name" ; $column_name = "//table column name" ; //connect to database $connect = mysql_connect("$server" , "$user" , "$password"); if (!$connect) { die('Could not connect: ' . mysql_error()); } //fetch contents of $column_name $query = "SELECT $column_name FROM $table_name "; $result = mysql_query($query); //echo results while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "{$row['$column_name']}" ; } ?> I hope this helps.
Prince at Yahoo! Answers Visit the source
Related Q & A:
- How can I insert posted data into the database?Best solution by Stack Overflow
- How can I change a value in an array?Best solution by Stack Overflow
- How can I insert image in my Yahoo profile?Best solution by Yahoo! Answers
- How can I insert HTML into my Yahoo Group description?Best solution by Yahoo! Answers
- How can I insert a picture in Email without attaching it?Best solution by Super User
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.