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

介绍

checkbox是一个非常有用的页面表单项,在让用户进行多重选择的情况下,它甚至可以允许用户选择全部项目或是一个都不选。但是,尽管这是一个非常优秀的表单元素,但在我们的工作中,在如何正确地保存选择项这方面总存在一些易混淆的情况发生。本文将描述在遵循好的数据库设计原则的方法下,如何把checkbox选择项正确地保存在数据库中。

要求

本文将阐述如何把选择项正确地保存在用户数据库中的方法。尽管这里包括了有用的PHP代码,但我将从数据库设计的观点来表达它们,所以,你可以很方便地使用任何一个数据库和服务器端脚本语言来实现。我只是想提供一个如何做的方法,让你能应用于你自己的站点中。如果你想运行这里的源码,你需要安装php、mysql和网络服务器。

例1:招聘站点

假如你被要求做一个招聘类的网站,允许求职的软件开发人员填写他们的技能,让雇主能访问这个站点并根据求职者的技能找到合适的员工。你也知道,一个开发人员拥有的技能会多于一个,因此你决定这样设计你的站点。

每一个求职者将允许访问本站,注册一个用户,并且输入他的技能,Checkbox就派上用场了,你可能想作这样的一页:

__ PHP __ MySQL __ Zope
__ Perl __ Javascript __ JSP

[提交]

每一个求职都可以选择他所拥有的技能。显然对于不同人来说这选择项是不同的。一个人可能会是PHP和Mysql,其它人可能只是JSP。你将如何保存这些选择呢?一个很自然的想法是针对每个选项建一个字段,这样开始可以正常工作。但是随后你可能会发现,当你想扩展或调整时,麻烦就来了,你可能不得不修改你的表结构。
好的方法应是这样的:

你应有一个用户表包含用户的注册信息,如用户名、密码和其它一些你需要的什么内容。假如你直接使用本文后面给出的源码,你要建一个简单的表如下:

id username
1 User1
2 User2
3 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 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的目的是提供从用户表到开发技能表之间的一个映射关系。换句话说,它让我们保存开发者和他们有的技能,如,当求职者完成选择点击提交时,我们将填写这个表用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 of
checkboxes */
$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 as
an 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 add
a 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 want
to 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;
}

? >

时间: 2024-07-28 13:28:02

PHP中在数据库中保存Checkbox数据(1)_php基础的相关文章

PHP中在数据库中保存Checkbox数据(2)_php基础

这代码是非常简单的,你很快地就看完了吧.主要的工作有两个函数完成:"get_checkbox_labels" 和 "make_checkbox_html".其中 "get_checkbox_labels" 查询表const_skills 并且返回一个对象数组,每一个对象有一个id值和相应的技能名称.我们传送这个数组和其它一些参数给"make_checkbox_html" ,这个函数将返回一个字串,用来生成checkbox的ht

.net中从数据库中查询数据的问题!!求高手指教~

问题描述 .net中从数据库中查询数据的问题!!求高手指教~ protected void Button1_Click(object sender EventArgs e) { string SQL = ""select * from AllItems where 1=1""; if (!string.IsNullOrEmpty(this.TextBox1.Text)) { SQL = SQL + "" and BianHao='"&q

dropdownlist-如何将DropDownList中从数据库中取到的数据,显示到Label

问题描述 如何将DropDownList中从数据库中取到的数据,显示到Label 专家姓名: DataSourceID="SqlDataSource1" DataTextField="PF_Name" DataValueField="PF_Name" onselectedindexchanged="DropDownList1_SelectedIndexChanged">/asp:DropDownList 专家ID:/as

access-如何在c#中从数据库中提取数据并将数据存在另一表中

问题描述 如何在c#中从数据库中提取数据并将数据存在另一表中 for (int i = 0; i < checkedListBox1.Items.Count; i++) { if (checkedListBox1.GetItemChecked(i)) { OleDbConnection cnn = new OleDbConnection(Form1.con); cnn.Open(); string aa; aa = Form1.su; string bb = "select c_no fr

在SQL SERVER中查询数据库中第几条至第几条之间的数据SQL语句写法_数据库其它

今天在写程序的时候,需要生成从开始id到结束id的sql语句.原来不需要这个功能现在就需要了. 在SQL SERVER中查询数据库中第几条至第几条之间的数据SQL语句如何写? 如:在SQL SERVER中查询数据库中第10条至30条之间的数据SQL语句如何写? ------解决方案-------------------- select top 20 * from 表 where id in (select top 30 id from 表 order by id)order by id desc

代码-dreamweaver cs6 怎么在框架中显示数据库中的某一条记录?

问题描述 dreamweaver cs6 怎么在框架中显示数据库中的某一条记录? 我在制作网页的时候陷入了瓶颈,现在希望能在dreamweaver中实现这个功能:点开一个链接,直接显示出数据库中的某一条记录,并且在下面的"推荐项目"后随机显示出其他的条目. 如果要实现这个功能,代码应该怎么写 解决方案 通过javascript等来访问数据库,获取数据,然后再显示到html页面 http://www.jb51.net/article/42178.htm 解决方案二: Dreamweave

JS中获取数据库中的值的方法

在项目中遇到一个问题,需要在JS中读取数据库中的值,然后再把值返回到页面中,解决方案如下:使用Ajax方法来实现,需要用到ajax.dll(一个ajax技术开发的帮助类库).   实施过程如下:     1.引用Ajax.dll     2.在App_Code写具体的方法,最好单独建立一个类文件,然后写具体方法.       复制代码 代码如下: public class AjaxMethod www.jb51.net {     public AjaxMethod()     {       

select-在C#的combobox中显示数据库中所有的表名,但是sql语句不知道怎么写

问题描述 在C#的combobox中显示数据库中所有的表名,但是sql语句不知道怎么写 List<string> tableNames = "Select( (string)x["Table_Name"] from dt where type=1 and flags=0)"; dt是连接数据库时设定的表名 解决方案 sql语句如下所示:SELECT NAME FROM SYSOBJECTS WHERE TYPE='U' 前提是提前设置好config配置文

如何thinkphp中从数据库中读取新闻的详细内容

问题描述 如何thinkphp中从数据库中读取新闻的详细内容 求救!!!如何thinkphp中从数据库中读取新闻的详细内容???急啊 解决方案 $m = M('表名') ; $m->where('id='.$id)->getField('新闻字段名') ; 解决方案二: 谢谢你的回答!!!!(*^__^*)