Page 1 of 1

checkbox/radio button error message PHP

Posted: Tue Jan 28, 2003 5:55 am
by jarow
have created an add record form (.PHP) that has 3 checkboxes. The checked value = Y, the initial state is unchecked. The form is sent to go to a view page in which all the entered data is viewed.

When I preview the form in a browser and check all 3 boxes and submit the form works perfectly; that is the data is entered correctly into the mysql database and the view page becomes visible.

If I preview the form and leave any of the boxes unchecked I get the following error message:

PHP Notice: Undefined index: endemismo in c:\inetpub\wwwroot\fauna_europaea\TMP826889f9hy.php on line 37
PHP Warning: Cannot modify header information - headers already sent in c:\inetpub\wwwroot\fauna_europaea\TMP826889f9hy.php on line 47

endemismo in this case is the name of the checkbox left unchecked.

It might also be worthy noting that the data is entered correctly into the database despite the error message, that is if the check box is checked the corresponding database field receives a "Y" and is blank if not checked.

Any ideas how to fix the error message?

Many thanks

Jim

Posted: Tue Jan 28, 2003 6:06 am
by DeGauss
What's going on in line 37 or 36?

The second error (in regards to header information) is because the error above it is being outputted by PHP and thus interrupting the programming flow.

Posted: Tue Jan 28, 2003 6:21 am
by jarow
This is the code that the error refers to:

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "add_form")) {
$insertSQL = sprintf("INSERT INTO mollusca (parentesis, taxondud, endemismo) VALUES (%s, %s, %s)",
GetSQLValueString($HTTP_POST_VARS['parentesis'], "text"),
GetSQLValueString($HTTP_POST_VARS['taxondud'], "text"),
GetSQLValueString($HTTP_POST_VARS['endemismo'], "text"));

The error message mentioned above was when field "endemismo" went unchecked. Line 37 corresponds to the GETSQLVALUE....line for endemismo

Posted: Tue Jan 28, 2003 6:30 am
by DeGauss
Hmm.

If endemismo is unchecked, then it would have a value of 0, right?

if so, and you have no error checks in place, sql is going to try and insert 0 into the database in the endemismo field.

This is bad. MySQL doesn't like treating 0 as a number. You have to enclose 0 in quotes (single or double, your call) to stop the script from barfing.

Maybe.

Yeah, after reading your first post, this is definately a maybe explanation ;)

Posted: Tue Jan 28, 2003 6:37 am
by twigletmac
If endemismo is unchecked then $HTTP_POST_VARS['endemismo'] won't exist and you'll get an error. So you need to do something that detects if $HTTP_POST_VARS['endemismo'] is set and if not assign it a default value so you can use it in your SQL statement.

Mac

Posted: Tue Jan 28, 2003 6:40 am
by DeGauss
twigletmac wrote:If endemismo is unchecked then $HTTP_POST_VARS['endemismo'] won't exist and you'll get an error.
Yeah, any radiobutton that is unchecked automatically has a value of 0, so it's not blank, it's just 0 which is as good as blank, but PHP doesn't think it's blank. Even though it is. Which it's not.

*head 'splode*

Posted: Tue Jan 28, 2003 6:50 am
by twigletmac
I think we're talking about checkboxes as opposed to radio buttons - checkboxes don't have a default 'off' state, they just don't return anything.

Mac

Posted: Tue Jan 28, 2003 6:53 am
by DeGauss
My bad. Brain not working on all cylinders this morning...