0

I'm trying to write a php code to have a webpage insert information from a FORM into a DB. I have managed to have the information from the form inserted correctly in the DB columns but when the user doesn't select ALL four items of the "checkbox" I get the following error message for every checkbox item not selected.

"Notice: Undefined index: ESPANOL in C:\xampp\htdocs\PHP\index1f.php on line 66"

I've been told the "isset" sentence could be the solution but I haven't been able to figure it out on my own.

HTML

<br>
Idiomas:<br>
ESPAÑOL:<INPUT type="checkbox" name="ESPANOL" value="s">
INGLES:<INPUT type="checkbox" name="INGLES" value="s"><br>
FRANCES:<INPUT type="checkbox" name="FRANCES" value="s">
PORTUGUES:<INPUT type="checkbox" name="PORTUGUES" value="s">
<br>

PHP

mysql_query("insert into alumnos2
(NOMBRE,APELLIDO,GENERO,ESTADO_CIVIL,ESTUDIOS,ESPANOL,INGLES,PORTUGUES,FRANCES,CLAVE)
    values('$_REQUEST[NOMBRE]','$_REQUEST[APELLIDO]','$_REQUEST[GENERO]','$_REQUEST[ESTADO_CIVIL    ]','$_REQUEST[ESTUDIOS]','$_REQUEST[ESPANOL]','$_REQUEST[INGLES]','$_REQUEST[PORTUGUES]','$_    REQUEST[FRANCES]','$_REQUEST[CLAVE]')",$x)

Note: the information is inserted in the database anyways.

FranX
  • 1
  • 1
  • are you trying to repopulate the selected values? – a7omiton Jul 06 '14 at 22:33
  • not really, I'm trying to insert an "s" in the DB for the checkboxes selected and nothing for the ones left untouched. – FranX Jul 06 '14 at 22:45
  • I just found this question was already answered but phrased differently :) thanks. http://stackoverflow.com/questions/476426/submit-an-html-form-with-empty-checkboxes – FranX Jul 06 '14 at 23:03

1 Answers1

0

Your query has a lot of columns that makes it hard to read. I would put the column names and values in an array and use that to construct the query. This approach also makes it easy to fill in only some values, and let the rest fall back on default values (such as NULL):

$values = array();

if (isset ($_REQUEST['ESPANOL'])) {
  $values['ESPANOL'] = "''"; /* Indicating true */
}
if (isset ($_REQUEST['INGLES'])) {
  $values['INGLES'] = "''";
}
/* ... */

$col_string = implode (',', array_keys ($values));
$val_string = implode (',', $values);

$query = "INSERT INTO alumnos2 ($col_string) VALUES ($val_string)";

Also:

You shouldn't use the deprecated mysql extention. Use PDO or mysqli instead.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42