Peter Hoskin wrote:

Chris Grigor wrote:

morning 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



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

Angelo

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to