PHP脚本数据库功能详解(中)
来源:互联网 发布:java 图片合成工具 编辑:程序博客网 时间:2024/06/04 19:52
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
数据库是数据组织、存储的中心。将要处理的也可能是各种数据,包括程序、文件、报表,甚至音频、视频数据。由于通过浏览器,个人用户只能填写少部分的个人简历。因此,我们这里示范用户个人简历上载的功能。其他类型的数据可以模仿此例进行操作。
首先是信息收集页面。让用户选择要上载的文件。此页面的html代码如下:
〈!-- begin of post.htm--〉
〈p〉 〈/p〉
〈form method="POST" action="insert.PHP" ENCTYPE="multipart/form-data"〉
〈p〉〈b〉个人简历提交〈/b〉〈/p〉
〈p〉姓名:〈br〉
〈input type="text" name="Name" size="20"〉〈/p〉
〈p〉个人简介:〈br〉
〈textarea rows="2" name="Intro" cols="20"〉〈/textarea〉〈/p〉
〈p〉简历文件:〈br〉
〈input type="file" name="ResuFile"〉〈/p〉
〈p〉〈input type="submit" value="提交" name="B1"〉〈/p〉
〈/form〉
〈!-End of post.htm--〉
注意,ENCTYPE关键字一定不能省,否则文件无法正确上载。
这里,我们再把向数据库插入记录的代码重新设计:
〈?
//begin of file insert.PHP
if($ResuFile != "none")
//确定用户选择了文件
{
$Size = filesize($ResuFile);
//确定文件大小
$mFileData = addslashes(fread(fopen($ResuFile, "r"), $Size));
//读取文件,对内容进行处理
unlink($ResuFile);
//删除上载临时文件
}
$LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");
$DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");
$query = "insert into Resume(Name,Intro,ResuFile) values('$Name', '$Intro', '$mFileData')";
$result = @mysql_query("$query",$LinkID); //执行查询,插入文件到数据库
if(! $result)
echo "数据插入失败!";
else
echo "文件上载成功!";
@mysql_close($LinkID);
//end of file insert.PHP
?〉
有了上面的基础,写出从数据库读数据的程序应该很简单了。需要注意的是文件向客户发送的方法。服务器必须向浏览器发送头信息,说明将要发送的数据为word文档。如果用户计算机装有MSWord,浏览器将自动调用word进行文档显示。
我们可以设置一个超级链接,来下载这个Word文件:
〈?
//begin of file show.PHP
$LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");
$DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");
$query = "insert into Resume(Name,Intro,ResuFile) values('$Name', '$Intro', '$mFileData')";
$result = @mysql_query("$query",$LinkID);
//执行查询,插入文件到数据库
$query= "select ID,Name,Intro from Resume";
//生成SQL语句
$result = mysql_query($query,$LinkID); //执行,结果集保存到变量$result中
$num= mysql_num_rows($result); //取得查询返回的记录行数
if($num == 0)
{
echo "没有找到任何记录";
exit();
}
while($row=mysql_fetch_array($result)) //取结果集的下一行数据到数组$row中
{
echo $row["ID"]." ".$row["Name"]." ".$row["Intro"]." ";
echo "〈a href= "download.PHP?ID=".$row["ID"].""〉查看Word文档〈/a〉〈br〉";
}
//end of file show.PHP
?〉
访问文件show.PHP,用户看到的是个人简要信息的列表。点击“查看Word文档”,即可看到对应成员详细的个人简历。
Word文档的显示是用下面的文件:
〈?
// begin of file download.PHP
$LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");
$DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");
$query = "select ResuFile from Resume where ID=$ID";
//$ID为调用传递的变量
$result = @mysql_query("$query",$LinkID);
//执行查询,从数据库读取文件内容
if(mysql_num_rows($result) 〈 1 )
{
echo "没有找到相应的文件!";
exit();
}
$row = mysql_fetch_array($result);
$mFileData = $row["ResuFile"];
//读取个人简历的内容(Word文件格式的数据)
header("Content-type: application/msword");
//发送头信息,说明将要发送的数据为word文档
echo $mFileData;
//发送文档数据
//end of file download.PHP
?〉
至此,我们已经实现了个人简历的提交、数据库存储、信息浏览等功能,基本完成了“人才信息交流”的框架功能。
需要说明的是,通过PHP进行文件上载及数据库存储是个较突出的技术难题。很多关于PHP的网站都不断出现这类问题。这些操作,对平台、环境设置依赖性较大。不同的平台配置,都可能导致操作的失败。本文后面附了上述程序的运行平台、编译参数,以供参考。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
利用PHP将文件保存到数据库 数据库是数据组织、存储的中心。将要处理的也可能是各种数据,包括程序、文件、报表,甚至音频、视频数据。由于通过浏览器,个人用户只能填写少部分的个人简历。因此,我们这里示范用户个人简历上载的功能。其他类型的数据可以模仿此例进行操作。
首先是信息收集页面。让用户选择要上载的文件。此页面的html代码如下:
〈!-- begin of post.htm--〉
〈p〉 〈/p〉
〈form method="POST" action="insert.PHP" ENCTYPE="multipart/form-data"〉
〈p〉〈b〉个人简历提交〈/b〉〈/p〉
〈p〉姓名:〈br〉
〈input type="text" name="Name" size="20"〉〈/p〉
〈p〉个人简介:〈br〉
〈textarea rows="2" name="Intro" cols="20"〉〈/textarea〉〈/p〉
〈p〉简历文件:〈br〉
〈input type="file" name="ResuFile"〉〈/p〉
〈p〉〈input type="submit" value="提交" name="B1"〉〈/p〉
〈/form〉
〈!-End of post.htm--〉
注意,ENCTYPE关键字一定不能省,否则文件无法正确上载。
这里,我们再把向数据库插入记录的代码重新设计:
〈?
//begin of file insert.PHP
if($ResuFile != "none")
//确定用户选择了文件
{
$Size = filesize($ResuFile);
//确定文件大小
$mFileData = addslashes(fread(fopen($ResuFile, "r"), $Size));
//读取文件,对内容进行处理
unlink($ResuFile);
//删除上载临时文件
}
$LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");
$DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");
$query = "insert into Resume(Name,Intro,ResuFile) values('$Name', '$Intro', '$mFileData')";
$result = @mysql_query("$query",$LinkID); //执行查询,插入文件到数据库
if(! $result)
echo "数据插入失败!";
else
echo "文件上载成功!";
@mysql_close($LinkID);
//end of file insert.PHP
?〉
有了上面的基础,写出从数据库读数据的程序应该很简单了。需要注意的是文件向客户发送的方法。服务器必须向浏览器发送头信息,说明将要发送的数据为word文档。如果用户计算机装有MSWord,浏览器将自动调用word进行文档显示。
我们可以设置一个超级链接,来下载这个Word文件:
〈?
//begin of file show.PHP
$LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");
$DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");
$query = "insert into Resume(Name,Intro,ResuFile) values('$Name', '$Intro', '$mFileData')";
$result = @mysql_query("$query",$LinkID);
//执行查询,插入文件到数据库
$query= "select ID,Name,Intro from Resume";
//生成SQL语句
$result = mysql_query($query,$LinkID); //执行,结果集保存到变量$result中
$num= mysql_num_rows($result); //取得查询返回的记录行数
if($num == 0)
{
echo "没有找到任何记录";
exit();
}
while($row=mysql_fetch_array($result)) //取结果集的下一行数据到数组$row中
{
echo $row["ID"]." ".$row["Name"]." ".$row["Intro"]." ";
echo "〈a href= "download.PHP?ID=".$row["ID"].""〉查看Word文档〈/a〉〈br〉";
}
//end of file show.PHP
?〉
访问文件show.PHP,用户看到的是个人简要信息的列表。点击“查看Word文档”,即可看到对应成员详细的个人简历。
Word文档的显示是用下面的文件:
〈?
// begin of file download.PHP
$LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");
$DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");
$query = "select ResuFile from Resume where ID=$ID";
//$ID为调用传递的变量
$result = @mysql_query("$query",$LinkID);
//执行查询,从数据库读取文件内容
if(mysql_num_rows($result) 〈 1 )
{
echo "没有找到相应的文件!";
exit();
}
$row = mysql_fetch_array($result);
$mFileData = $row["ResuFile"];
//读取个人简历的内容(Word文件格式的数据)
header("Content-type: application/msword");
//发送头信息,说明将要发送的数据为word文档
echo $mFileData;
//发送文档数据
//end of file download.PHP
?〉
至此,我们已经实现了个人简历的提交、数据库存储、信息浏览等功能,基本完成了“人才信息交流”的框架功能。
需要说明的是,通过PHP进行文件上载及数据库存储是个较突出的技术难题。很多关于PHP的网站都不断出现这类问题。这些操作,对平台、环境设置依赖性较大。不同的平台配置,都可能导致操作的失败。本文后面附了上述程序的运行平台、编译参数,以供参考。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- PHP脚本数据库功能详解(中)
- PHP脚本数据库功能详解(下)
- PHP脚本数据库功能详解(上)
- 在PHP脚本中操作MYSQL数据库
- php数据库备份脚本
- PHP 自 5.2 到 7.0 中新增的功能详解
- PHP 自 5.2 到 5.6 中新增的功能详解
- PHP 自 5.2 到 5.6 中新增的功能详解
- PHP 自 5.2 到 5.6 中新增的功能详解
- PHP 自 5.2 到 5.6 中新增的功能详解
- 使用 PHP脚本 创建数据库
- 数据库中脚本问题
- PHP 调用python 脚本实现python功能
- PHP操作数据库详解
- php+mysql mysqli_fetch_row详解 从数据库中循环取数据
- PHP 留言簿(带Oracle数据库分页的显示功能)
- Oracle数据库ASM功能详解
- Oracle数据库ASM功能详解
- 在客户端验证密码强度[2],兼容FireFox和IE
- PHP脚本数据库功能详解(下)
- .Net编程的多个小技巧
- 如何在WIN2K下安装PHP4.04
- 编程技巧OOPs:复制构造函数
- PHP脚本数据库功能详解(中)
- 编程技巧:.NetFramework
- PHP4.04简明安装
- 存储过程编写经验和优化措施
- PHP脚本数据库功能详解(上)
- 无刷新仿google波形扭曲彩色Asp.net验证码
- 在PWS上安装PHP4.0正式版
- GIS开发随笔--GIS技术的一点理解和MapNet控件试验
- 在IIS上安装PHP4.0正式版