简体   繁体   English

PHP / HTML表单转换为DB复选框验证

[英]PHP/HTML form into DB checkbox validation

I'm trying to write a php code to have a webpage insert information from a FORM into a DB. 我正在尝试编写php代码,以使网页将信息从FORM插入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. 我设法将来自表单的信息正确地插入到DB列中,但是当用户未选择“复选框”的所有四个项时,对于未选中的每个复选框项,我都会收到以下错误消息。

"Notice: Undefined index: ESPANOL in C:\\xampp\\htdocs\\PHP\\index1f.php on line 66" “通知:未定义的索引:第66行的C:\\ xampp \\ htdocs \\ PHP \\ index1f.php中的ESPANOL”

I've been told the "isset" sentence could be the solution but I haven't been able to figure it out on my own. 有人告诉我“ isset”语句可能是解决方案,但我自己还无法弄清楚。

HTML 的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 的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. 注意:信息仍然会插入数据库中。

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): 这种方法还使仅填写一些值变得容易,而让其余的保留默认值(例如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. 您不应该使用已弃用的mysql扩展。 Use PDO or mysqli instead. 改用PDOmysqli

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM