Re: Is there an easier way of doing this?
| From: | Angelo Zanetti | Date: | Wed, 10 May 2006 09:17:14 +0000 |
| Subject: | Re: Is there an easier way of doing this? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-235866@lists.php.net to get a copy of this message | ||
Peter Hoskin wrote:
Chris Grigor wrote:why not make the checkbox an array checkbox and just loop through the values that come through the POST and insert those into the DB. I would definitely recommend using integer rather than what you have above for the DB value. HTH Angelomorning all, Is there an easier way of doing the following?? form1 submitting to form1.php <input type="checkbox" name="1"> <input type="checkbox" name="2"> <input type="submit> form1.php <?php $link = mysql_connect('host', 'user', 'pass') or die ("Connection failed:" . mysql_error()); mysql_select_db('yourdbname', $link) or die ("selection failed: " . mysql_error()); if(isset($_POST[1])) { mysql_query("INSERT INTO menu (label) VALUES ('item 1 selected')"); } else { mysql_query("INSERT INTO menu (label) VALUES ('item 1 not selected')"); } if(isset($_POST[2])) { mysql_query("INSERT INTO menu (label) VALUES ('item 2 selected')"); } else { mysql_query("INSERT INTO menu (label) VALUES ('item 2 not selected')"); } mysql_close($link); ?>Thats a very poor method of storing values in SQL. I believe MySQL now has a boolean datatype (at long last, postgre has had it for ages) and it does have integers. Why not a combination of a boolean and an integer, and no storage of text. It makes a hell of alot more sense.So my question is, if I have a form with 20 + items which can be checkboxes, when submitted do I need to go through each one and add it to the datasbase or maybe some kind of loop?.http://pear.php.net/package/HTML_QuickForm/ may be of use. You could use a combination of exportValues() and foreach. Its added validation makes it even nicer. Also, I do believe your reference to $_POST is incorrect. What if the variable does not exist? You get an error, thats what. array_key_exists('1',$_POST) should be used. Regards, Peter Hoskin