av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語(yǔ)言

PHP中在數(shù)據(jù)庫(kù)中保存Checkbox數(shù)據(jù)

時(shí)間:2025-05-16 01:04:15 php語(yǔ)言 我要投稿
  • 相關(guān)推薦

PHP中在數(shù)據(jù)庫(kù)中保存Checkbox數(shù)據(jù)

  checkbox是一個(gè)非常有用的頁(yè)面表單項(xiàng),在讓用戶進(jìn)行多重選擇的情況下,它甚至可以允許用戶選擇全部項(xiàng)目或是一個(gè)都不選。但是,盡管這是一個(gè)非常優(yōu)秀的表單元素,但在我們的工作中,在如何正確地保存選擇項(xiàng)這方面總存在一些易混淆的情況發(fā)生。本文將描述在遵循好的數(shù)據(jù)庫(kù)設(shè)計(jì)原則的方法下,如何把checkbox選擇項(xiàng)正確地保存在數(shù)據(jù)庫(kù)中。

  要求

  本文將闡述如何把選擇項(xiàng)正確地保存在用戶數(shù)據(jù)庫(kù)中的方法。盡管這里包括了有用的PHP代碼,但我將從數(shù)據(jù)庫(kù)設(shè)計(jì)的觀點(diǎn)來表達(dá)它們,所以,你可以很方便地使用任何一個(gè)數(shù)據(jù)庫(kù)和服務(wù)器端腳本語(yǔ)言來實(shí)現(xiàn)。我只是想提供一個(gè)如何做的方法,讓你能應(yīng)用于你自己的站點(diǎn)中。如果你想運(yùn)行這里的源碼,你需要安裝php、mysql和網(wǎng)絡(luò)服務(wù)器。

  例1:招聘站點(diǎn)

  假如你被要求做一個(gè)招聘類的網(wǎng)站,允許求職的軟件開發(fā)人員填寫他們的技能,讓雇主能訪問這個(gè)站點(diǎn)并根據(jù)求職者的技能找到合適的員工。你也知道,一個(gè)開發(fā)人員擁有的技能會(huì)多于一個(gè),因此你決定這樣設(shè)計(jì)你的站點(diǎn)。

  每一個(gè)求職者將允許訪問本站,注冊(cè)一個(gè)用戶,并且輸入他的技能,Checkbox就派上用場(chǎng)了,你可能想作這樣的一頁(yè):

  __ PHP __ MySQL __ Zope

  __ Perl __ Javascript __ JSP

  [提交]

  每一個(gè)求職都可以選擇他所擁有的技能。顯然對(duì)于不同人來說這選擇項(xiàng)是不同的。一個(gè)人可能會(huì)是PHP和Mysql,其它人可能只是JSP。你將如何保存這些選擇呢?一個(gè)很自然的想法是針對(duì)每個(gè)選項(xiàng)建一個(gè)字段,這樣開始可以正常工作。但是隨后你可能會(huì)發(fā)現(xiàn),當(dāng)你想擴(kuò)展或調(diào)整時(shí),麻煩就來了,你可能不得不修改你的表結(jié)構(gòu)。

  好的方法應(yīng)是這樣的:

  你應(yīng)有一個(gè)用戶表包含用戶的注冊(cè)信息,如用戶名、密碼和其它一些你需要的什么內(nèi)容。假如你直接使用本文后面給出的源碼,你要建一個(gè)簡(jiǎn)單的表如下:

  id username

  1 User1

  2 User2

  3 User3

  我們先建一個(gè)表 "const_skills" 用如下的 SQL 語(yǔ)句:

  SQL> CREATE TABLE const_skills (

  id int not null primary key,

  value varchar(20) );

  現(xiàn)在我們加入技能:

  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 現(xiàn)在應(yīng)是這樣的:

  id value

  1 PHP

  2 MySQL

  3 Zope

  4 Perl

  5 Javascript

  6 JSP

  這個(gè)表只是讓用戶可以選擇相應(yīng)的技能,現(xiàn)在,再建一個(gè)表 lookup_skills 用如下的SQL:

  SQL> CREATE TABLE lookup_skills (

  id int not null auto_increment primary key,

  uid int,

  skill_id int );

  這個(gè)表lookup_skills的目的是提供從用戶表到開發(fā)技能表之間的一個(gè)映射關(guān)系。換句話說,它讓我們保存開發(fā)者和他們有的技能,如,當(dāng)求職者完成選擇點(diǎn)擊提交時(shí),我們將填寫這個(gè)表用checkbox中被選定的那些值。對(duì)于每一個(gè)選上的技能,我們?cè)谶@個(gè)表中加一條記錄,記下用戶id及所選項(xiàng)的id。(想必大家都清楚了吧。我譯到這,嘿嘿…)

  在我們看這個(gè)插入記錄的代碼之前,我們先設(shè)計(jì)一下這個(gè)頁(yè)面,應(yīng)有的內(nèi)容有一個(gè)表單,我們可以查詢的數(shù)據(jù)庫(kù)并且取checkbox標(biāo)簽從const_skills表中,建這個(gè)checkbox表單項(xiàng)。

  代碼如下:

  < ?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;

  }

  ? >

【PHP中在數(shù)據(jù)庫(kù)中保存Checkbox數(shù)據(jù)】相關(guān)文章:

php向Mysql數(shù)據(jù)庫(kù)保存數(shù)據(jù)的代碼04-24

PHP向MySQL數(shù)據(jù)庫(kù)保存數(shù)據(jù)代碼03-21

如何在PHP中連接MySQL數(shù)據(jù)庫(kù)04-13

PHP使用中數(shù)據(jù)庫(kù)使用方法05-21

PHP 中 MySQL 數(shù)據(jù)庫(kù)異步查詢實(shí)現(xiàn)03-03

PHP訪問數(shù)據(jù)庫(kù)04-27

PHP5中使用PDO連接數(shù)據(jù)庫(kù)的方法03-13

php數(shù)據(jù)庫(kù)備份腳本05-25

PHP怎么插入數(shù)據(jù)庫(kù)07-09