Re: Is there an easier way of doing this?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux