search
HomeBackend DevelopmentPHP Tutorialphp录入页面中动态从数据库中提取数据的实现_PHP

php录入页面中动态从数据库中提取数据的实现_PHP

Jun 01, 2016 pm 12:34 PM
phpinformationdynamicaccomplishextractdatadatabasepage

石家庄师范专科学校 计算中心  张书梅

摘要:用PHP制作动态Web页面时,在提交服务器之前,让PHP根据用户在当前页面上录入的某字段的值立即从数据库中取出相关的其它字段的值并显示到当前页面上,是PHP程序开发中的难点。本文以一个具体实例详细介绍了怎样将两个HTML内嵌式语言PHP和JavaScript巧妙结合起来,解决这个难点的具体方法。

关键词:PHP、动态、HTML。

现在的网站已经从以前提供静态信息的形式发展到交互式的提供动态信息业务的方式。Web的信息服务形式可以概括为两点:向客户提供信息;记录客户提交的信息。要提供这两种服务,需解决的问题是:如何快速地让用户在自己网站大量的信息中快速提取他想要的信息,如何有效地记录用户提交的信息,以便于将来用户查找。这些问题都可以通过在网站中加入数据库支持来解决。

因PHP对多种数据库都能提供良好的支持,且PHP的脚本直接嵌入在HTML文档中,使用非常方便。因此PHP是现在Internet上最流行的一种Server端嵌入语言之一。另外,与其它的Server端脚本语言如ASP相比,PHP免费开放源码并且提供跨平台的支持,这使它能够轻易适应当今网络中各种异质的网络环境;可让网页制作人员能够非常快捷、方便地制作出功能强大的动态Web页面。但是,由于PHP是服务器端嵌入,更直观的理解是PHP语句是在服务器上执行,所以它只有提交时才将当前页面上的内容接收和处理。而当你需要的内容是根据客户当前页面上录入的某字段的值,再动态到库中提取时,PHP就无能为力了。例如:要向客户提供一个“订货合同”的录入页面,其中包含一些“供货商信息”的录入,而各供货商的详细信息事先已在一个“商家”字典表中录入,现在要求当客户在当前页面上选中某“供货商”时,立时从“商家”字典表中将该供货商的某些信息如“开户行、帐号、地址、电话”等提取出来显示到当前页面上供客户直接使用或修改使用。这样的要求若用可视化编程语言如PB、VB等实现是一件轻而易举的事,但PB、VB不适合编写动态Web页面;PHP适合编写动态Web页面,但由于是服务器端嵌入,不能及时获得提交前页面上的变量值,所以实现上述要求就有一定的难度。在程序编写过程中,我将PHP与JavaScript巧妙结合起来,解决了这个难点。

我们知道,同样是嵌入语句,但是JavaScript 又不同于PHP语言。因为PHP是服务器端嵌入,而JavaScript是客户端嵌入,既JavaScript语句是在客户的浏览器上执行,这就决定了JavaScript可及时获得当前页面上的变量值,但又无法对服务器端的数据库直接操作。所以,将二者结合起来制作出功能强大的动态Web页面,可谓是珠联璧合。为了叙述方便,下面只以从字典表中取出选中供货商的地址为例,说明具体做法。当需要取出多个字段时,方法类似,但利用JavaScript函数从字符串中逐个取出时,要细心一些。

1.编写一个PHP函数

此函数的功能是将所有符合条件的“供货商信息”从“商家”字典表中取出,并存放到一个字符串变量$khsz中。

   function Khqk_Tq($questr){

     global $dbconn;

     $dbq_resl=sybase_query($questr,$dbconn);  //送出一个query字符串供Sybase执行。

     $dbq_rows=sybase_num_rows($dbq_resl);    //获得返回行的数目。

     $j=0;

for ($i=0;$i

       $k[]=sybase_result($dbq_resl,$i,"kh_id");  //取出用户选择的供货商编号。

       $add=sybase_result($dbq_resl,$i,"address");  //取出该供货商地址。

       if ($add==""):

          $k[]="无";

       else :

         $k[]=sybase_result($dbq_resl,$i,"address");

     endif;

     $khsz=$khsz.$k[$j]."|".$k[$j+1]."|";  //将各字段值以”|”为分隔符,连接到变量$khsz 中,形成一个长字符串。

       $j=$j+2;

   }

  return $khsz;

}

2.编写一个JavaScript函数

该函数的功能是从字符串中根据kh_id值找到该供货商的地址,嵌入到HTML文件中。

  var  khstr="=$k?>"               //先将PHP变量转变成JavaScript变量khstr。

 function khxz_onclick(){

   frm=document.frmplanfill;

   ghstj=frm.kh_id.value;          //获得当前页面上刚刚选中的“供货商”的kh_id值。

   numkh=khstr.indexOf(ghstj,0) ; //从khstr串中找到该kh_id值所在的位置。

addr=khstr.substring(khstr.indexOf("|",numkh)+1,khstr.indexOf("|",khstr.indexOf("|",numkh)+1));           //从 khstr串中取出与kh_id对应的地址字段的值。

   frm.address.value=addr;  //将取出的值赋给当前录入页面上的字段变量address。

}

3.在HTML中将二者结合起来,互为所用

 $khinfo="select kh_id,address from kh where co_id=$s_coid and type='G' order by kh_id";

//将取供货商信息的SQL语句放到变量$khinfo中。

  $k=Khqk_Tq($khinfo);    //调用PHP函数,并将返回的字符串值放到变量$k中。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.