PHP中在数据库中保存Checkbox数据(1)

来源:互联网 发布:营销数据分析常用公式 编辑:程序博客网 时间:2024/05/07 16:44
在数据库中保存Checkbox数据(1) 介绍  checkbox是一个非常有用的页面表单项,在让用户进行多重选择的情况下,它甚至可以允许用户选择全部项目或是一个都不选。但是,尽管这是一个非常优秀的表单元素,但在我们的工作中,在如何正确地保存选择项这方面总存在一些易混淆的情况发生。本文将描述在遵循好的数据库设计原则的方法下,如何把 checkbox选择项正确地保存在数据库中。要求  本文将阐述如何把选择项正确地保存在用户数据库中的方法。尽管这里包括了有用的PHP代码,但我将从数据库设计的观点来表达它们,所以,你可以很方便地使用任何一个数据库和服务器端脚本语言来实现。我只是想提供一个如何做的方法,让你能应用于你自己的站点中。如果你想运行这里的源码,你需要安装 php、mysql和网络服务器。例1:招聘站点  假如你被要求做一个招聘类的网站,允许求职的软件开发人员填写他们的技能,让雇主能访问这个站点并根据求职者的技能找到合适的员工。你也知道,一个开发人员拥有的技能会多于一个,因此你决定这样设计你的站点。  每一个求职者将允许访问本站,注册一个用户,并且输入他的技能,Checkbox就派上用场了,你可能想作这样的一页:__ PHP __ MySQL __ Zope__ Perl __ Javascript __ JSP[提交]  每一个求职都可以选择他所拥有的技能。显然对于不同人来说这选择项是不同的。一个人可能会是PHP和Mysql,其它人可能只是JSP。你将如何保存这些选择呢?一个很自然的想法是针对每个选项建一个字段,这样开始可以正常工作。但是随后你可能会发现,当你想扩展或调整时,麻烦就来了,你可能不得不修改你的表结构。好的方法应是这样的:  你应有一个用户表包含用户的注册信息,如用户名、密码和其它一些你需要的什么内容。假如你直接使用本文后面给出的源码,你要建一个简单的表如下:id username1 User12 User23 User3我们先建一个表 "const_skills" 用如下的 SQL 语句: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 value1 PHP2 MySQL3 Zope4 Perl5 Javascript6 JSP这个表只是让用户可以选择相应的技能,现在,再建一个表 lookup_skills 用如下的SQL:SQL> CREATE TABLE lookup_skills (id int not null auto_increment primary key,uid int,skill_id int );  这个表lookup_skills的目的是提供从用户表到开发技能表之间的一个映射关系。换句话说,它让我们保存开发者和他们有的技能,如,当求职者完成选择点击提交时,我们将填写这个表用checkbox中被选定的那些值。对于每一个选上的技能,我们在这个表中加一条记录,记下用户id及所选项的 id。(想必大家都清楚了吧。我译到这,嘿嘿…) 在我们看这个插入记录的代码之前,我们先设计一下这个页面,应有的内容有一个表单,我们可以查询的数据库并且取checkbox标签从const_skills表中,建这个checkbox表单项。代码如下:< ?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 >< ?phpfunction 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;}? >[第一节完]----------------------------作者:Dan LaFlamme译者:sharetop(ycshowtop@21cn.com)来源: www.phpbuilder.net