I am new to PHP and am working on my first project. I have a form that requires a user to pick at least two of his favourite colours (checkbox options) see html code below, how can I save these checkbox options into mysql using php? And how can I display these information from the database? _____________________________________________________________- <html> <head> <title>FAVOURITE COLOURS</title> </head> <body> <form method="POST" action="specify_file.php"> <table border="0" width="60%" id="table1" cellspacing="0" cellpadding="0"> <tr> <td colspan="2"> <p align="center"><b>FAVOURITE COLOURS</b></td> </tr> <tr> <td width="25%">Name</td> <td width="75%"><input type="text" name="T1" size="20"></td> </tr> <tr> <td width="25%">Company:</td> <td width="75%"><input type="text" name="T2" size="20"></td> </tr> <tr> <td width="25%">E-mail:</td> <td width="75%"><input type="text" name="T3" size="20"></td> </tr> <tr> <td width="100%" colspan="2"><b>Choose at least 2 of your best colours</b></td> </tr> <tr> <td width="25%">Red</td> <td width="75%"> <input type="checkbox" name="chkColours[]" value="red"></td> </tr> <tr> <td width="25%">Blue</td> <td width="75%"> <input type="checkbox" name="chkColours[]" value="Blue"></td> </tr> <tr> <td width="25%">Green</td> <td width="75%"> <input type="checkbox" name="chkColours[]" value="Green"></td> </tr> <tr> <td width="25%">Yellow</td> <td width="75%"> <input type="checkbox" name="chkColours[]" value="Yellow"></td> </tr> </table> <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> </body> </html> __________________________________________ Thanks
<?php $colors = serialize($chkColours); # insert $colors variable into mysql # select colors field from mysql $colors = unserialize($row['colors']); echo "Selected colors:<br />\n"; foreach ($colors as $val) { echo "<li>$val</li>\n"; } ?> PHP:
As you are using array for your checkbox, this will tell you how to capture checked value. http://www.plus2net.com/php_tutorial/array_checkbox.php This will display the ( only )checked values so within that while loop write inset query for storeing the value in table. Once your records are stored then displaying part is simple, you will get a basic tutorial on how to display from mysql here http://www.plus2net.com/php_tutorial/php_mysql_data_display.php