PHP学习模块之留言板

是看着网上视频做的,没有用数据库,因为代码写的比较乱,先摘出来,回来再添加功能

html部分 

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>留言板</title>
</head>
<body>
	<center>
		<h1>我的留言板</h1>
		<a href="index.html">添加留言</a>|
		<a href="show.php">查看留言</a>
		<hr width="90%"/>
		<div>
			<h3>添加留言</h3>
		</div>
		<div>
		<form action="doAdd.php" method="post" >
			<table border="0" cellpadding="4">
			<tr>
				<td align="right">标题:</td>
				<td><input type="text" name="title"></td>
			</tr>
			<tr>
				<td align="right">留言者:</td>
				<td><input type="text" name="author"></td>
			</tr>
			<tr>
				<td align="right">留言内容:</td>
				<td><textarea  rows="8" cols="30" name="content"></textarea></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input type="reset" name="重置"></td>
			</tr>
		</form>
	</div>
	</center>
	

</body>
</html>

添加:doAdd.php

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>留言板</title>
</head>
<body>
	<center>
		<h1 id="title">我的留言板</h1>
		<a href="index.html">添加留言</a>|
		<a href="show.php">查看留言</a>
		<hr width="90%"/>
		<div>
			<h3>执行添加留言</h3>
		</div>
		<?php
		//执行文件添加操作
		//1.获取留言信息
			$title=$_POST["title"];
			$author=$_POST["author"];
			$content=$_POST["content"];
			$time=time();//获取时间戳
		
		//2.拼装留言信息
		if($title != null){
		$sql="{$title}##{$author}##{$content}##{$time}";
		//echo $sql;
		file_put_contents("./liuyan.db",$sql."@@@",FILE_APPEND);
		}
		else{
			echo "头标题不允许为空,<a href='index.html'>返回主页</a>";
		}
		//3.将留言信息写入到liuyan.db中
		//4.提示留言成功
		?>
		
			
	
	</center>
	

</body>
</html>

删除:delete.php:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>留言板</title>
</head>
<body>
	<center>
		<h1 id="title">我的留言板</h1>
		<a href="index.html">添加留言</a>|
		<a href="show.php">查看留言</a>
		<hr width="90%"/>
		<div>
			<h3>执行留言信息删除</h3>
		</div>
		<?php
		//先读出来,再把数据给抠出来,再返回去
		//1.获取要删除的留言id号
		$id=$_GET["id"];
		//2.读取文件数据
		$file=file_get_contents("./liuyan.db");
		//去掉拼接符
		$file=rtrim($file,"@");
		$list=explode("@@@",$file);
		unset($list[$id]);
		file_put_contents("./liuyan.db",implode("@@@",$list)."@@@");
		echo "删除成功!";
		?>
		
	</center>
	

</body>
</html>

 添加:show.php

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>留言板</title>
</head>
<body>
	<center>
		<h1 id="title">我的留言板</h1>
		<a href="index.html">添加留言</a>|
		<a href="show.php">查看留言</a>
		<hr width="90%"/>
		<table border="1" width="700">
			<tr>
				<th>留言标题</th>
				<th>留言者</th>
				<th>留言内容</th>
				<th>添加时间</th>
				<th>操作</th>
			</tr>
			<?php
			//获取信息并展示
			//1.获取留言信息
			$file = file_get_contents('./liuyan.db');
			//2.拆分file信息
			$file=rtrim($file,"@");
			$list=explode("@@@",$file);
			//var_dump($list);
			//遍历每条留言信息
			foreach($list as $value=>$v){
				//var_dump($value);
				$ly=explode("##",$v);
				
				echo "<tr>";
				echo "<td>{$ly[0]}</td>";
				echo "<td>{$ly[1]}</td>";
				echo "<td>{$ly[2]}</td>";
				echo "<td>".date("Y-m-d H:i",$ly[3])."</td>";
				//echo "<td><a href="">删除</a></td>";
				echo "<td><a href='delete.php?id={$value}'>删除</a>";
				echo"</tr>";
				
			}
			

			?>
			}
		</table>
	</center>
	

</body>
</html>

猜你喜欢

转载自blog.csdn.net/sinat_35161044/article/details/82154988