引言
引言
复选框(Checkbox)是网页表单中极具价值的控件,尤其在需要用户进行多项选择的场景中,它支持用户全选或全不选。然而,尽管该元素功能强大,在实际开发中,关于如何正确存储这些选中值往往存在混淆。本文将阐述在遵循规范数据库设计原则的基础上,如何将复选框的选择结果准确持久化到数据库中。
需求分析
需求分析
本文旨在阐明将选中项正确存入用户数据库的方法。虽然文中包含实用的PHP代码片段,但核心视角侧重于数据库设计层面,因此读者可轻松将其适配至任何数据库系统及服务器端脚本语言。本文意在提供一种通用的方法论,供开发者应用于自身项目中。若需运行示例源码,环境需具备PHP、MySQL及Web服务器。
案例背景:招聘网站
例1:招聘站点1
假设你需要开发一个招聘类网站,允许软件开发者填写其掌握的技能,以便雇主能根据技能匹配求职者。鉴于开发人员通常拥有多项技能,我们需要为此设计相应的数据存储方案。
每位求职者注册并输入技能时,复选框便派上用场。你可能希望构建如下界面:
__ PHP __ MySQL __ Zope
__ Perl __ Javascript __ JSP
[提交]
每位求职者可选择其所拥有的技能,且不同人的选择各异。例如,一人可能选择PHP和MySQL,另一人仅选JSP。如何保存这些选择?一种直观的想法是为每个选项创建一个字段,初期可行,但后续扩展或调整时会遇到麻烦,可能需要修改表结构。
数据库结构设计
更优的方案如下:
首先建立一个用户表,用于存储用户名、密码等注册信息。若直接使用本文后续提供的源码,需创建如下简单表:
id username
1 User1
2 User2
3 User3
接着,使用以下SQL语句创建"const_skills"表:
SQL> CREATE TABLE const_skills (
id int not null primary key,
value varchar(20) );
随后插入技能数据:
SQL> INSERT INTO const_skills(id, value) VALUES (1, "PHP");
SQL> INSERT INTO const_skills(id, value) VALUES (2, "MySQL");
SQL> INSERT INTO const_skills(id, value) VALUES (3, "Zope");
SQL> INSERT INTO const_skills(id, value) VALUES (4, "Perl");
SQL> INSERT INTO const_skills(id, value) VALUES (5, "Javascript");
SQL> INSERT INTO const_skills(id, value) VALUES (6, "JSP");
此时,const_skills表内容如下:
id value
1 PHP
2 MySQL
3 Zope
4 Perl
5 Javascript
6 JSP
此表仅用于提供可选技能列表。接下来,创建lookup_skills表,SQL语句如下:
SQL> CREATE TABLE lookup_skills (
id int not null auto_increment primary key,
uid int,
skill_id int );
lookup_skills表的作用是在用户表与技能表之间建立映射关系。换言之,它用于保存开发者及其拥有的技能。当求职者完成选择并提交时,我们将向该表填充数据,记录被选中的复选框值。对于每个选中的技能,在该表中添加一条记录,包含用户ID和技能ID。
前端表单与代码实现
在查看插入记录的代码之前,我们先设计页面。页面应包含一个表单,查询数据库并从const_skills表中获取复选框标签以构建选项。
< ?php
/* insert code to connect to your database here */
/* get the checkbox labels */
$skills = get_checkbox_labels("const_skills");
/* create the html code for a formatted set ofcheckboxes */
$html_skills = make_checkbox_html($skills, 3, 400, "skills[]");? > < html >
< body >
< br >
< form name="skills" method="POST" action="insertskills.php" >
Check off your web development skills:
< ? echo "$html_skills"; ? >
< br >
< input type="submit" value="Submit" >
< /form >
< /body >
< /html >
< ?php
function get_checkbox_labels($table_name) {
/* make an array */
$arr = array();
/* construct the query */
$query = "SELECT * FROM $table_name";
/* execute the query */
$qid = mysql_query($query);
/* each row in the result set will be packaged asan object and put in an array */
while($row= mysql_fetch_object($qid)) {
array_push($arr, $row);
}
return $arr;
}
/* Prints a nicely formatted table of checkbox choices.
$arr is an array of objects that contain the choices
$num is the number of elements wide we display in the table
$width is the value of the width parameter to the table tag
$name is the name of the checkbox array
$checked is an array of element names that should be checked
*/
function make_checkbox_html($arr, $num, $width, $name, $checked) {
/* create string to hold out html */
$str = "";
/* make it */
$str .= "< table width="$width" border="0" >n";
$str .= "< tr >n";
/* determine if we will have to close adda closing tr tag at the end of our table */
if (count($arr) % $num != 0) {
$closingTR = true;
}
$i = 1;
if (isset($checked)) {
/* if we passed in an array of the checkboxes we wantto be displayed as checked */
foreach ($arr as $ele) {
$str .= "< td >< input type="checkbox" name="$name" value="$ele- >id"";
foreach ($checked as $entry) {
if ($entry == $ele- >value) {
$str .= "checked";
continue;
}
}
$str .= " >";
$str .= "$ele- >value";
if ($i % $num == 0) {
$str .= "< /tr >n< tr >";
} else {
$str .= "< /td >n";
}$i++;
}
} else {
/* we just want to print the checkboxes. none will have checks */
foreach ($arr as $ele) {
$str .= "< td >< input type="checkbox" name="$name" value="$ele- >id" >";
$str .= "$ele- >value";
if ($i % $num == 0) {
$str .= "< /tr >n< tr >";
} else {
$str .= "< /td >n";
}$i++;
}
}
/* tack on a closing tr tag if necessary */
if ($closingTR == true) {
$str .= "< /tr >< /table >n";
} else {
$str .= "< /table >n";
}
return $str;
}? >