《PHP动态网页》
实训指导书
上饶职业技术学院
信息工程系
目录
实训1:PHP开发环境安装... 1
实训2:PHP基础(一)... 5
实训3:PHP基础(二)... 7
实训4:PHP数据处理... 11
实训5: PHP Web项目实践... 16
实训6: PHP和数据库... 24
实训1:PHP开发环境安装
实训名称: PHP开发环境安装 成绩:
实训日期: 年 月 日 实训报告日期: 年 月 日
一、实训目的
1、掌握 Apache服务环境安装和配置
2、掌握PHP环境安装和配置
3、初步接触PHP程序
二、实训内容
1、安装和配置Apache服务环境
2、安装和配置PHP环境
3、使用Adobe Dreamweaver CS5开发简单的PHP程序
三、实训环境
Apache服务器安装包程序、MySql数据库安装包程序、MySql数据库管理程序phpMyAdmin、php程序、Adobe Dreamweaver CS5
四、实训步骤、过程
1、安装Apache服务器
双击文件httpd-2.2.19-win32-x86-openssl-0.9.8r.msi,在安装程序打开窗口中,除了制定安装目录,并设定输入主机名、管理员信箱,见图1.1。
图1.1
一路选择“Next”下去,完成安装。注(在此我们将Apache服务器安装在D:/apache/apache2.2)
安装完成后,在任务栏托盘区中会增加一个Apache服务程序运行图标,这说明服务器已经安装成功,在IE地址栏中键入http://localhost/,即可看到Apache的欢迎页面。
2、设置Apache的服务端口
在Apache安装路径下的D:\Apache\Apache2.2\conf\文件夹中,打开httpd.conf文件,找到“listen 80”所在的一行,将“80”改为自己希望的端口号,如8000。修改后,停止Apache服务,再启动,然后再浏览器地址栏中输入http://localhost:8000,观察效果。
3、安装PHP环境
将php也解压缩在d:/apache/php目录下
4、配置PHP参数
(1)在步骤2所用到的httpd.conf文件中,在文件最后加上以下三行代码
PHPIniDir "D:\Apache\php"
LoadModule php5_module "D:\Apache\php\php5apache2_2.dll"
AddType application/x-httpd-php .php
其中:
①PHPIniDir " "一行的“”中,填写为PHP的安装路径。
②LoadModule php5_module " "一行的" ",填写为PHP的安装路径\ php5apache2_2.dll。
(2)设置PHP文件存放路径
打开httpd.conf文件,在文件最后加上
<Directory "D:\Apache\wwwroot"> (D:\Apache\wwwroot是放置web目录的路径,路径即是存放网站的路径)
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
(3)配置启用默认文档
打开D:\Apache\Apache2.2\conf\httpd.conf
<IfModule dir_module> 238行附近,设置启用默认文档
DirectoryIndex index.html index.php
</IfModule>
(4)配置虚拟目录
①打开httpd.conf文件,启用Include conf/extra/httpd-vhosts.conf(461行附近)
②打开Apache\Apache2.2\conf\extra中的httpd-vhosts.conf文件并进行配置,即设置虚拟目录与域名,在D盘的Apache文件夹下创建wwwroot文件夹。
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.locahost
DocumentRoot "D:\Apache\wwwroot\test"
ServerName test
ErrorLog "D:\Apache\wwwroot\test\logs\error.log"
CustomLog "D:\Apache\wwwroot\test\logs\access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.locahost
DocumentRoot "D:\Apache\wwwroot\phpMyAdmin"
ServerName mysql
ErrorLog "D:\Apache\wwwroot\phpMyAdmin\logs\error.log"
CustomLog "D:\Apache\wwwroot\phpMyAdmin\logs\access.log" common
</VirtualHost>
注:以上的logs文件夹是存放错误日志的文件夹
(5)找到PHP的安装路径下的php.ini(复制一份php.ini-dist,文件名修改为php.ini
),启用配置
找到:
extension_dir = "D:\Apache\php\ext" (491行附近,此行说明要调用ext中的文件)
session.save_path = "D:\Apache\tmp"(session临时保存的文件夹,其中tmp是自行创建的,995行附近)
extension=php_gd2.dll(前面的";"去掉)启用618行附近
extension=php_mysql.dll(前面的";"去掉) 启用633行附近
upload_tmp_dir = "D:\Apache\tmp"(上传文件临时保存的文件夹,其中tmp是自行创建的)549行附近
(6)将PHP安装路径下的文件php5ts.dll,libmysql.dll复制到Winnt(Windows)/system32/目录中。
5、安装Mysql在Apache文件夹下,安装详细步骤见MySQL详细安装步骤.doc文档。
6、配置Mysql数据库的管理程序phpMyAdmin
①将phpMyAdmin整个文件夹复制到wwwroot文件夹下,phpMyAdmin是用来管理数据库MySQL的程序;
②在wwwroot创建一个名为test的文件夹,该文件夹即是存放网站的文件夹;
7、配置C:\WINDOWS\system32\drivers\etc下的hosts文件,即域名和本地IP(127.0.0.1)一致。
8、创建一个简单的PHP程序,保存为index.php,并将其存放于test文件夹下,在地址栏里输入http://test/,如果输出hello, world!,说明环境配置正确,效果如图1.2所示:
<html>
<head>
<title>First program</title>
</head>
<body>
<?php
echo "hello, world!";
?>
</body>
</html>
图1.2
9、上述的(1)、(2)、(3)、(4)、(5)、(6)、(7)都做完后,重新启动Apache服务。
五、练习
按照实训步骤把PHP运行环境配置好,并编写简单的PHP动态网页。
六、源程序清单、测试数据、结果
七、实训出现的问题、实训结果分析(语法错英语提示,中文翻译,原因,您的理解?)
实训2:PHP基础(一)
实训名称: PHP基础 成绩:
实训日期: 年 月 日 实训报告日期: 年 月 日
一、实训目的
1、掌握PHP语法基本元素,掌握数据类型、变量和常量、运算符、表达式的使用
2、掌握PHP流程控制
3、掌握在Html和PHP命令标记相结合的方法
4、掌握用PHP和Html交互的处理方法
二、实训内容
1、PHP语法:数据类型、变量和常量、运算符、表达式、流程控制
2、PHP和html交互
三、实训环境
PHP开发环境、Adobe Dreamweaver CS5
四、实训步骤、过程
1、在html中嵌入PHP命令标记
实验任务:编写一个php动态页面,在html标记中用先嵌入一段php代码,给变量$xh赋一个文本数值;然后把$xh的数值作为一个html表单中的文本型输入框的value属性值。
编程示例:
<html>
<head>
<title>在html中嵌入PHP命令</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<h1>PHP inside html</h1>
<?php
$xh="081101";
?>
<form action="" method="post">
学号是<input type="text" name="xh" size="20" value="<?php echo $xh;?>">
</form>
</body>
</html>
2、PHP语法实验
(1)变量、表达式和判断的使用
实验任务:编写一段PHP代码,用于判断一个整数变量的数值是否大于5,并显示判断结果。
编程示例:
<?
echo "<br>";
$i=10;
if($i>5)
echo "i大于5<br>";
else
echo "i不大于5<br>";
?>
(2)循环
实验任务:在(1)所编写PHP代码的基础上,添加一段循环,从1依次显示到整数变量的数值,各数之间以“,”做分隔符。
编程示例:
<?
echo "<br>";
$i=10;
if($i>5)
echo "i大于5<br>";
else
echo "i不大于5<br>";
for($j=1;$j<=$i;$j++)
{
If($j<$i)
echo $j.",";
else
echo $j;
}
?>
3、php读取表单数值
实验任务:编写一个带Form和输入控件的PHP页面,用PHP代码接收输入控件的内容,并显示。
编程示例:
<html>
<head>
<title> PHP读取表单练习</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<h1>PHP读取表单练习</h1>
<form action="" method="post">
请输入变量$i的数值<input type="text" name="i" size="20">
<input type="submit" name="submit" value="确定">
</form>
<?php
if(isset($_POST['submit']))
{
$i=$_POST['i'];
$i=(int)$i;
if($i>5)
echo " <script>alert('i 大于5');</script> ";
else
echo " <script>alert('i 不大于5');</script> ";
for($j=0;$j<$i;$j++)
{
if($j==$i-1)
echo $j;
else
echo $j.",";
}
}
?>
</body>
</html>
五、练习
编写程序完成:100个和尚吃100个馒头.大和尚一人吃3个,小和尚3人吃一个.大,小和尚各多少人?
六、源程序清单、测试数据、结果
七、实训出现的问题、实训结果分析(语法错英语提示,中文翻译,原因,您的理解?)
实训3:PHP基础(二)
实训名称: PHP基础 成绩:
实训日期: 年 月 日 实训报告日期: 年 月 日
一、实训目的
1、掌握php中函数的定义和使用方法
2、掌握php中类的定义和使用方法
二、实训内容
1、PHP函数
2、PHP面向对象编程
三、实训环境
PHP开发环境、Adobe Dreamweaver CS5
四、实训步骤、过程
1、函数的定义和使用
实验任务:设计一个PHP网页ex3_1.php,其中定义一个PHP函数,用于比较前两个输入参数的大小。若第三个输入参数的数值是“B”,就将最大的数值返回,若第三个参数的数值是“L”,就将最小的数值返回,若前两个输入参数一样大,则返回二者其中之一。并用同一个PHP网页输入两个数值,调用上述的函数返回结果。
编程示例:
<html>
<head>
<title> PHP函数练习</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<?php
function cbl($i,$j,$p)
{
if($i>=$j)
{
$bigger=$i;
$littler=$j;
}
else{
$bigger=$j;
$littler=$i;
}
if($p=="B") return $bigger;
else return $littler;
}
if(isset($_POST['submit']))
{
$a=$_POST['a'];
$a=(int)$a;
$b=$_POST['b'];
$b=(int)$b;
$sel=$_POST['sel'];
}
?>
<h1>PHP函数练习</h1>
<form action="" method="post">
<table width="80%" border="0">
<tr>
<td width="20%">
请输入变量$a的数值</td>
<td width="80%"><input type="text" name="a" size="20" value="<?php echo $a;?>"></td>
<tr>
<tr>
<td>
请输入变量$b的数值</td>
<td><input type="text" name="b" size="20" value="<?php echo $b;?>"></td>
<tr>
<tr>
<td>指定返回数值是</td>
<td>
<select name="sel">
<option value="最大值">最大值</option>
<option value="最小值">最小值</option>
</select>
</td>
<tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="确定"></td>
<tr>
<tr>
<td>结果是 </td>
<td>
<?php
if($sel=="最大值")
$control="B";
else
$control="L";
echo "两者的".$sel."是".cbl($a,$b,$control);
?>
</td>
<tr>
</table>
</form>
</body>
</html>
2、类的定义和使用
实验任务:在一个PHP网页ex3_2.php中,设计一个学生管理类,有学号、姓名、专业等属性,用来存储学生的信息。用PHP代码创建学生管理类的实例,并用输入文本框给实例的属性赋值,并显示实例的属性数值。
编程示例:
<html>
<head>
<title>PHP面向对象设计练习</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$sid=$_POST['sid'];
$sname=$_POST['sname'];
$spel=$_POST['spel'];
}
class student
{
private $sid;
private $sname;
private $spel;
function show($xh,$xm,$zy)
{
$this->sid=$xh;
$this->sname=$xm;
$this->spel=$zy;
echo "学号:".$this->sid."<br>";
echo "姓名:".$this->sname."<br>";
echo "专业:".$this->spel."<br>";
}
}
?>
<h1>PHP类的设计练习</h1>
<form action="" method="post">
<table width="80%" border="0">
<tr>
<td width="10%">
请输入学号:</td>
<td width="80%"><input type="text" name="sid" size="20" value="<?php echo $sid;?>"></td>
<tr>
<tr>
<td>
请输入姓名</td>
<td><input type="text" name="sname" size="20" value="<?php echo $sname;?>"></td>
<tr>
<tr>
<td>请指定专业</td>
<td>
<select name="spel">
<option value="软件设计">软件设计</option>
<option value="信息管理">信息管理</option>
</select>
</td>
<tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="确定"></td>
<tr>
<tr>
<td>实例是 </td>
<td>
<?php
$stu=new student();
$stu->show($sid,$sname,$spel);
//echo $sid;
?>
</td>
<tr>
</table>
</form>
</body>
</html>
五、练习
六、源程序清单、测试数据、结果
七、实训出现的问题、实训结果分析(语法错英语提示,中文翻译,原因,您的理解?)
实训4:PHP数据处理
实训名称: PHP数据处理 成绩:
实训日期: 年 月 日 实训报告日期: 年 月 日
一、实训目的
1、掌握PHP中处理数组数据的方法
2、掌握PHP中字符串操作的方法
3、掌握PHP中正则表达式的使用方法
4、掌握PHP中文件的操作方法
5、掌握PHP中日期数据的处理方法
二、实训内容
1、使用PHP数组:包括定义、初始化、键和值、定位和遍历
2、进行字符串操作
3、用正则表达式验证表单数据正确性
4、文件打开、关闭、写入、读出等操作
5、日期函数的使用
三、实训环境
PHP开发环境、Adobe Dreamweaver CS5
四、实训步骤、过程
1、数组的操作
实验任务:设计一个PHP网页ex4_1.php,其中使用循环将用户输入的5个数由小到大排序显示
编程示例:
<?php
echo "请输入需要排序的数据:<br>";
echo "<form method='post'>";
for($i=1;$i<6;$i++)
{
echo "<input type='text' name='seq[]' size='5'>";
if($i<5)
echo "-";
}
echo "<input type='submit' name='confirm' value='提交'>";
echo "</form>";
?>
<?php
if(isset($_POST['confirm']))
{
$temp=0;
$seq=$_POST['seq'];
$num=count($seq);
echo "您输入的数据有:<br>";
foreach($seq as $score)
{
echo $score."<br>";
}
for($i=0;$i<$num;$i++){
for($j=$i+1;$j<$num;$j++){
if($seq[$j]>$seq[$i])
{
$temp=$seq[$j];
$seq[$j]=$seq[$i];
$seq[$i]=$temp;
}
}
}
echo "从大到小排序后的结果是:<br>";
while(list($key,$value)=each($seq))
{
echo $value."<br>";
}
}
?>
2、字符串的操作
实验任务:设计一个PHP网页ex4_2.php,输入5个学生的学号,如果有相同的学号则只保留一个,找到前缀为“0811” 的学生,将前缀改为“0810”,最后将所有学号输出,以逗号“,”为分隔符。
编程示例:
<?php
echo "请输入学生的学号:<br>";
echo "<form method='post'>";
for($i=1;$i<6;$i++)
{
echo "<input type='text' name='stu[]' size='5'>";
if($i<5)
echo "-";
}
echo "<input type='submit' name='confirm' value='提交'>";
echo "</form>";
?>
<?php
if(isset($_POST['confirm']))
{
$k=0;
$jsj=array();
$stu=$_POST['stu'];
for($i=0;$i<count($stu);$i++){
for($j=$i+1;$j<count($stu);$j++)
{
if(strcmp($stu[$i],$stu[$j])==0)
array_splice($stu,$j,1); //删除重复元素
}
}
$str=implode(",",$stu);//将数组转换为字符串
echo "所有学生的学号如下:";
echo $str."<br>";
foreach($stu as $value){
if(strstr($value,"0811"))
{
$string=str_replace("0811","0810",$value);
$jsj[$k]=$string;
$k++;
}
}
echo "调整后,学生的学号如下:<br>";
echo implode(",",$jsj);
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
</body>
</html>
3、上传文件,要求能创建以当前日期为文件名的文件夹存放文件,能控制上传文件的类型及大小。
图4.1 上传图
<?php
header("Content-Type; text/html; charset=utf-8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table width="352" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="352" align="center">文件上传</td>
</tr>
<tr>
<td><form action="" method="post" enctype="multipart/form-data" name="FORM" id="FORM">
<input name="task" type="hidden" id="add" value="add" />
<label>
<input type="file" name="userFile" id="userFile" />
</label>
<label>
<input type="submit" name="button" id="button" value="上传" />
</label>
</form></td>
</tr>
</table>
</body>
</html>
<?php
if ($_POST['task']=='add'){
//创建目录的函数:mkdir 0777
$houZui=array("doc","docx","ppt","pptx","zip","rar","jpg","txt");
$dir="upload/".date("Y-m-d");
$maxFile=2*1024*1024;
if(!file_exists($dir)){
mkdir($dir,'0777');
}
if($_FILES['userFile']['name']!=''){
if ($_FILES['userFile']['size']<=$maxFile){
$pic=$_FILES['userFile']['name'];
$a=explode(".",$pic);
$houZuiMing=strtolower($a[count($a)-1]);
if(in_array($houZuiMing,$houZui)){
$name=$_FILES['userFile']['name'];
$picture=$dir."/".time().".".$houZuiMing;
if(move_uploaded_file($_FILES['userFile']['tmp_name'],$picture)){
echo "上传成功!";
}else{
echo "上传失败!";
}
}else{
echo "<script>alert('格式不支持!');window.location='add.php'</script>";
}
}else{
echo "<script>alert('文件太大!');window.location='add.php'</script>";
}
}else{
echo "<script>alert('不能为空!');window.location='add.php'</script>";
}
}
?>
五、练习
六、源程序清单、测试数据、结果
七、实训出现的问题、实训结果分析(语法错英语提示,中文翻译,原因,您的理解?)
实训5: PHP Web项目实践
——注册登录系统的设计与开发
实训名称: PHP Web项目实践 成绩:
实训日期: 年 月 日 实训报告日期: 年 月 日
一、实训目的
1、掌握使用PHP编写交互网站所需要的方法
2、包括接收表单数据
3、使用会话
4、掌握数据库MySql的操作
二、实训内容
1、连接数据库
2、接收表单数据
3、页面跳转
4、使用会话
5、对数据库MySql进行数据删除、增加
三、实训环境
PHP开发环境、Adobe Dreamweaver CS5
四、实训步骤
实验任务:编写一个小型注册登录系统,当用户注册登录之后,能显示其相对应的个人信息,在注册提交时,能判断其所填信息不能为空,将所做的所有页面放在一个以register命名的文件夹中。
编程示例:
1、进入MySql,创建一个名为login的数据库,并新建一个名为com_users的数据表,数据表中的字段和字段属性如下表5.1所示:
5.1 表com_users字段名和字段属性
2、连接数据库页面config.php
<?php
//Database information
define("DB_HOSTNAME", "127.0.0.1");//localhost
define("DB_DATABASE_NAME", "login");//数据库存名称
define("DB_USERNAME", "root");
define("DB_PASSWORD", "123456");
$fileDir=substr(dirname(__FILE__), 0, -7);
$link=mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD)or die(" Database connect false");
mysql_select_db(DB_DATABASE_NAME,$link);
mysql_query("SET names utf8");
mysql_query("SET SQL_MODE=''");//设置模式
@date_default_timezone_set("Asia/Shanghai");
?>
3、头部head.php页面
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板</title>
<script language="JavaScript" type="text/JavaScript">
function submitForm(reg){
var f = document.adminForm;
if(f.username.value==''){
alert("请输入用户名");
f.username.focus();
return;
}
if(f.email.value==''){
alert("请输入电子邮箱");
f.email.focus();
return;
}
if(f.password.value==''){
alert("请输入密码");
f.password.focus();
return;
}
if(f.repassword.value==''){
alert("请输入重复密码");
f.repassword.focus();
return;
}
if(f.sex.value==''){
alert("请选择性别");
f.sex.focus();
return;
}
if(f.mobile.value==''){
alert("请输入手机号");
f.mobile.focus();
return;
}
f.task.value=reg;
f.submit();
}
</script>
<style type="text/css">
body,td,th {
font-size: 12px;
}
</style>
</head>
<body>
<table width="700" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><a href="register.php">用户注册</a> |
<?php
if($_SESSION['id']>0){
echo $_SESSION['username'].",您好!<a href='login.php?task=out'>退出</a>";
}else{
?>
<a href="login.php">用户登录</a>
<?php
}
?>
</td>
</tr>
</table><br><br />
4、注册与显示详细信息页面register.php
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
include_once("config.php");
include_once("head.php");
$id=$_SESSION['id'];
if($_SESSION['id']>0){
$sql="select * from com_users where id=".$id;
$rs=mysql_query($sql);
//$obj=mysql_fetch_object($rs);
//$gbtitle=$obj->gbtitle;
$row=mysql_fetch_array($rs,MYSQL_BOTH);
$username=$row['username'];
$email=$row['email'];
$password=$row['password'];
$repassword=$row['repassword'];
$sex=$row['sex'];
$mobile=$row['mobile'];
}
?>
<table width="616" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top:10px">
<form action="reg.php" method="post" name="adminForm" id="adminForm">
<input type="hidden" id="task" name="task" value="reg">
<tr>
<td height="35" colspan="2" align="center">用户注册 </td>
</tr>
<tr>
<td width="140" height="39" align="right" bgcolor="#eeeeee">用 户 名:</td>
<td width="560" height="39" align="left" bgcolor="#eeeeee"><span class="cheng12">
<input name="username" type="text" id="username" size="20" value="<?php echo $username;?>" />
</span><span class="hong12">* 最多30个字符</span></td>
</tr>
<tr>
<td height="35" align="right"> 电子邮箱:</td>
<td height="35" align="left"><input name="email" type="text" id="email" value="<?php echo $email;?>" size="20" />
<span class="hong12">* </span></td>
</tr>
<tr>
<td height="35" align="right" bgcolor="#eeeeee"> 密 码:</td>
<td height="35" align="left" bgcolor="#eeeeee"><input name="password" type="password" id="password" value="<?php echo $password;?>" size="20" />
<span class="hong12">* 最多15个字符</span></td>
</tr>
<tr>
<td height="35" align="right"> 重复密码:</td>
<td height="35" align="left"><input name="repassword" type="password" id="repassword" value="<?php echo $repassword;?>" size="20" />
<span class="hong12">*</span></td>
</tr>
<tr>
<td height="35" align="right" bgcolor="#eeeeee">性 别: </td>
<td height="35" align="left" bgcolor="#eeeeee">
<?php
if($sex==0){
?>
<input name="sex" type="radio" id="radio" value="0" checked="checked" />
男
<label for="sex">
<input type="radio" name="sex" id="radio2" value="1" />
女
<input type="radio" name="sex" id="radio3" value="2" />
保密</label><?php }?>
<?php
if($sex==1){
?>
<input name="sex" type="radio" id="radio" value="0"/>
男
<label for="sex">
<input type="radio" name="sex" id="radio2" value="1" checked="checked" />
女
<input type="radio" name="sex" id="radio3" value="2" />
保密</label><?php }?>
<?php
if($sex==2){
?>
<input name="sex" type="radio" id="radio" value="0"/>
男
<label for="sex">
<input type="radio" name="sex" id="radio2" value="1" />
女
<input type="radio" name="sex" id="radio3" value="2" checked="checked" />
保密</label><?php }?>
</td>
</tr>
<tr>
<td height="35" align="right">手 机: </td>
<td height="35" align="left"><input name="mobile" type="text" class="shuRuKuang" id="mobile" value="<?php echo $mobile;?>" /></td>
</tr>
<tr>
<td height="35" align="center" bgcolor="#eeeeee"> </td>
<td height="35" align="left" bgcolor="#eeeeee"><input type="button" name="button" value="提 交" onClick="javascript:submitForm('reg');">
<input type="reset" name="button2" id="button2" value="重 置" />
<input type="button" name="button2" id="button2" value="返 回" onClick="window.location='login.php'" /></td>
</tr>
<tr>
<td height="49" colspan="2" align="left" style="line-height:16px"><p class="hui12"> </p></td>
</tr>
</form>
</table>
</body>
</html>
5、注册添加页面reg.php
<?php
header("content-type:text/html;charset=utf-8");
session_start();
include_once('config.php');
if($_POST['task']=="reg"){
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
$repassword=$_POST['repassword'];
$sex=$_POST['sex'];
$mobile=$_POST['mobile'];
if($password!=$repassword){
echo "<script>alert('两次输入的密码不一致!');</script>";
echo "<script>window.location='register.html';</script>";
exit();
}
$sql="select * from com_users where username='".$username."'";
$rs=mysql_query($sql);
$num=mysql_num_rows($rs);
if($num>0){
//用户存在
echo "<script>alert('该用户名已存在,请更换用户名再注册!');</script>";
echo "<script>window.location='register.php';</script>";
exit();
}
$regtime=time();
$published=1;
$password=md5($password);
$sql="insert into com_users(username,password,email,regtime,published,mobile,sex) values('$username','$password','$email','$regtime','$published','$mobile','$sex')";
if(mysql_query($sql)){
echo "<script>alert('注册成功');</script>";
echo "<script>window.location='login.php';</script>";
exit();
}else{
echo "<script>alert('注册失败!');</script>";
echo "<script>window.location='register.html';</script>";
exit();
}
}
?>
6、登录页面与退出页面login.php
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
include_once("config.php");
//第一步是否提交
if($_POST['task']=='login'){
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql="select * from com_users where username='".$username."' and password='".$password."'";
$rs=mysql_query($sql);
$num=mysql_num_rows($rs);
if($num>0){
//用户存在
$obj=mysql_fetch_object($rs);
$id=$obj->id;
$username=$obj->username;
$_SESSION['id']=$id;
$_SESSION['username']=$username;
if($_POST['remember']==1){
setcookie("username",$username,time()+365*24*3600,'/');
//echo $_COOKIE['username'];
//exit();
}
header("Location:register.php");
}else{
echo "<script>alert('用户名或者密码不正确!');window.location='login.php'</script>";
exit();
}
}
if($_GET['task']=="out"){
$_SESSION['id']='';
$_SESSION['username']='';
}
include_once("head.php");
?>
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#000000">
<form action="login.php" name="login" method="post">
<input type="hidden" name="task" value="login" />
<tr bgcolor="#999999">
<td colspan="2" align="center"><strong>管理员登录</strong></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="153" align="right">用户名:</td>
<td width="447"><label for="username"></label>
<input type="text" name="username" id="username" value="<?php echo $_COOKIE['username'];?>"/></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right">密码:</td>
<td><label for="password"></label>
<input type="password" name="password" id="password" /></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right"></td>
<td><input type="checkbox" name="remember" value="1" />记住我</td>
</tr>
<tr bgcolor="#CCCCCC">
<td colspan="2" align="center"><input type="submit" name="sub" id="登录" value="登录" /></td>
</tr>
</form>
</table>
</body>
</html>
五、练习
六、源程序清单、测试数据、结果
七、实训出现的问题、实训结果分析(语法错英语提示,中文翻译,原因,您的理解?)
实训6: PHP和数据库
——留言板系统的设计与开发
实训名称: PHP 和数据库 成绩:
实训日期: 年 月 日 实训报告日期: 年 月 日
一、实训目的
掌握PHP连接和操作数据库的方法
二、实训内容
1、连接数据库
2、接收表单数据
3、页面跳转
4、使用会话
5、对数据库MySql进行数据删除、增加
6、用PHP连接和操作MySql的方法
三、实训环境
PHP开发环境、Adobe Dreamweaver CS5
四、实训步骤
实验任务:编写一个留言板程序,普通用户只有查看留言的功能,而管理员具有回复和删除留言板的功能,并且要求具有分页功能,每页只显示三条留言内容。
编程示例:
1、将common文件夹放在根目录(Gbook)下面,其中里面包含了分页的文件pageturn.inc.php和db.inc.php。
2、进入MySql,创建一个名为Gbook的数据库,并新建两个名为gbook和user的数据表,数据表中的字段和字段属性如下表所示:
表6.1 gbook表的字段和字段属性
表6.2 user表的字段和字段属性
3、连接数据库页面
<?php
//Database information
define("DB_HOSTNAME", "127.0.0.1");//localhost
define("DB_DATABASE_NAME", "gbook1");//数据库存名称
define("DB_USERNAME", "root");
define("DB_PASSWORD", "123456");
$fileDir=substr(dirname(__FILE__), 0, -7);
$link=mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD)or die(" Database connect false");
mysql_select_db(DB_DATABASE_NAME,$link);
mysql_query("SET names utf8");
mysql_query("SET SQL_MODE=''");//设置模式
@date_default_timezone_set("Asia/Shanghai");
?>
4、头部head.php页面
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板</title>
<style type="text/css">
body,td,th {
font-size: 12px;
}
</style>
</head>
<body>
<table width="700" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><a href="index.php">首页</a> | <a href="add.php">我要留言</a> |
<?php
if($_SESSION['id']>0){
echo $_SESSION['username'].",您好!<a href='login.php?task=out'>退出</a>";
}else{
?>
<a href="login.php">管理员登录</a>
<?php
}
?>
</td>
</tr>
</table><br><br />
5、首页与删除页面index.php
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
include_once("config.php");
include_once("common/pageturn.inc.php");
if($_GET['task']=="delete"&&intval($_GET['id'])>0){
if($_SESSION['id']<=0){
echo "<script>alert('请先登录!');window.location='login.php'</script>";
exit();
}
$id=intval($_GET['id']);
$sql="delete from gbook where id=".$id;
if(mysql_query($sql)){
echo "<script>alert('删除成功');window.location='index.php'</script>";
exit();
}else{
echo "<script>alert('删除失败!');window.location='index.php'</script>";
exit();
}
}
?>
<?php
include_once("head.php");
$sql="select * from gbook order by id DESC";
$obj=new Lwgpageturn(3); //将对象实例化
$obj->query($sql);
//下面单列显示记录示例
for ($i=0;$i<$obj->shownum;$i++){
?>
<table width="700" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#000000">
<tr bgcolor="#FFFFFF">
<td width="120">标题:<?php echo $obj->Records[$i]['gbtitle'];?></td>
<td width="203">时间:<?php echo date("Y-m-d H:i:s",$obj->Records[$i]['addtime']); ?></td>
<td width="243">IP:<?php echo $obj->Records[$i]['ip']; ?></td>
</tr>
<tr bgcolor="#FFFFFF">
<td rowspan="2"><p>留言人<?php echo $obj->Records[$i]['username']; ?></p>
<p>电话:<?php echo $obj->Records[$i]['phone'] ?></p></td>
<td height="132" colspan="2" valign="top"> <?php
echo $obj->Records[$i]['gbcontent'];
if($obj->Records[$i]['replycontent']!=''){
echo "<br><b>管理员回复:</b>".$obj->Records[$i]['replycontent'];
}
?></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="30" colspan="2" align="right" valign="top"><a href="add.php?id=<?php echo $obj->Records[$i]['id'];?>">回复</a> | <a href="index.php?id=<?php echo $obj->Records[$i]['id'];?>&task=delete">删除</a> </td>
</tr>
</table>
<p>
<?php
echo "<br>";
}
?>
</p>
<table width="633" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td><?php echo $obj->navbar(10,2,false,"#000000").$obj->pagejump(); ?></td>
</tr>
</table>
</body>
</html>
6、回复与添加页面add.php
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
include_once("config.php");
if($_POST['task']=='add'){
$gbtitle=$_POST['gbtitle'];
$gbcontent=$_POST['gbcontent'];
$username=$_POST['username'];
$phone=$_POST['phone'];
$ip=$_SERVER['REMOTE_ADDR'];
$addtime=time();
$sql="insert into gbook(gbtitle,gbcontent,username,phone,ip,addtime) values ('$gbtitle','$gbcontent','$username','$phone','$ip','$addtime')";
if(mysql_query($sql)){
echo "<script>alert('留言成功');window.location='index.php'</script>";
}else{
echo "<script>alert('留言失败!');window.location='index.php'</script>";
}
}
if($_POST['task']=="edit"){//回复操作
if($_SESSION['id']<=0){
echo "<script>alert('请先登录!');window.location='login.php'</script>";
exit();
}
$id=intval($_POST['id']);
$replycontent=$_POST['replycontent'];
$sql="update gbook set replycontent='".$replycontent."',replyer='0',replytime='".time()."' where id=".$id;
if(mysql_query($sql)){
echo "<script>alert('回复成功');window.location='index.php'</script>";
exit();
}else{
echo "<script>alert('回复成功');window.location='index.php'</script>";
exit();
}
}
include_once("head.php");
$id=intval($_REQUEST['id']);
if($id>0){
if($_SESSION['id']<=0){
$refer=urlencode("add.php?id=".$id);
echo "<script>alert('请先登录!');window.location='login.php?refer=".$refer."'</script>";
exit();
}
$sql=" select * from gbook where id=".$id;
$rs=mysql_query($sql);
//$obj=mysql_fetch_object($rs);
//$gbtitle=$obj->gbtitle;
$row=mysql_fetch_array($rs,MYSQL_BOTH);
$gbtitle=$row['gbtitle'];
$username=$row['username'];
$phone=$row['phone'];
$gbcontent=$row['gbcontent'];
$replycontent=$row['replycontent'];
$id=$row['id'];
$task="edit";
$taskname="回复";
}else{
$task="add";
$taskname="留言";
}
?>
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#000000">
<form action="add.php" method="post">
<input type="hidden" name="task" value="<?php echo $task;?>" />
<input type="hidden" name="id" value="<?php echo $id;?>" />
<tr bgcolor="#FFFFFF">
<td colspan="2" align="center"><strong>我要留言</strong></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="138" align="right">标题</td>
<td width="462"><label for="gbtitle"></label>
<input type="text" name="gbtitle" value="<?php echo $gbtitle;?>" id="gbtitle" /></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right">姓名</td>
<td><label for="uername"></label>
<input type="text" name="username" id="username" value="<?php echo $username;?>"/></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right">电话</td>
<td><label for="phone"></label>
<input type="text" name="phone" id="phone" value="<?php echo $phone;?>"/></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right">留言内容</td>
<td><label for="gbcontent"></label>
<textarea name="gbcontent" id="gbcontent" cols="45" rows="5"><?php echo $gbcontent;?></textarea></td>
</tr>
<?php
if($id>0){
?>
<tr bgcolor="#FFFFFF">
<td align="right">回复内容</td>
<td><label for="replycontent"></label>
<textarea name="replycontent" id="replycontent" cols="45" rows="5"><?php echo $replycontent;?></textarea></td>
</tr>
<?php
}
?>
<tr bgcolor="#FFFFFF">
<td align="right"> </td>
<td><input type="submit" name="sub" value="<?php echo $taskname;?>" /></td>
</tr>
</form>
</table>
</body>
</html>
7、管理员登录页面login.php
<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
include_once("config.php");
//第一步是否提交
if($_POST['task']=='login'){
$username=$_POST['username'];
$password=$_POST['password'];
$sql="select * from user where username='".$username."' and password='".$password."'";
$rs=mysql_query($sql);
$num=mysql_num_rows($rs);
if($num>0){
//用户存在
$obj=mysql_fetch_object($rs);
$id=$obj->id;
$username=$obj->username;
$_SESSION['id']=$id;
$_SESSION['username']=$username;
//$UArr=array('id'=>$id,'name'=>$username);
//$U=implode("|",$UArr);
if($_POST['remember']==1){
setcookie("username",$username,time()+365*24*3600,'/');
//echo $_COOKIE['username'];
//exit();
}
if($_POST['refer']!=''){
header("Location:".$_POST['refer']);
}else{
header("Location:index.php");
}
exit();
}else{
echo "<script>alert('用户名或者密码不正确!');window.location='login.php'</script>";
exit();
}
}
if($_GET['task']=="out"){
$_SESSION['id']='';
$_SESSION['username']='';
}
include_once("head.php");
?>
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#000000">
<form action="login.php" name="login" method="post">
<input type="hidden" name="task" value="login" />
<input type="hidden" name="refer" value="<?php echo $_GET['refer']?>" />
<tr bgcolor="#999999">
<td colspan="2" align="center"><strong>管理员登录</strong></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="153" align="right">用户名:</td>
<td width="447"><label for="username"></label>
<input type="text" name="username" id="username" value="<?php echo $_COOKIE['username'];?>"/></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right">密码:</td>
<td><label for="password"></label>
<input type="password" name="password" id="password" /></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right"></td>
<td><input type="checkbox" name="remember" value="1" />记住我</td>
</tr>
<tr bgcolor="#CCCCCC">
<td colspan="2" align="center"><input type="submit" name="sub" id="登录" value="登录" /></td>
</tr>
</form>
</table>
</body>
</html>
五、练习
六、源程序清单、测试数据、结果
七、实训出现的问题、实训结果分析(语法错英语提示,中文翻译,原因,您的理解?)