简易留言板(数据库交互)
2016-02-24 23:52
453 查看
刚开始学php自己在文本式留言板的基础上琢磨出来的。
link.php
<?php
$link=mysqli_connect("localhost","root","6464");
mysqli_query($link,"create database liuyan character set utf8 ");
$sql=create table table1(
title varchar(30),
author varchar(10),
content varchar(100),
time varchar(30),
ip varchar(10),
id smallint unsigned auto_increment
)charset utf8 engine innodb;
mysqli_query($link,$sql);
?>
menu.php
<h1 color="red">我的留言板</h1>
<a href="index.php">添加留言</a>
<a href="show.php">查看留言</a>
<hr width="100"/>
index.php
<html>
<head>
<meta charset="utf-8">
<title>我的留言板</title> <?php //初始页面,用于收集留言并发送到doADD.php里?>
</head>
<body>
<center>
<?php include("menu.php");?>
<?php include("link.php");?>
<form action="doAdd.php" method="post">
<table width="400">
<h3>添加留言</h3>
<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="3" cols="17" name="content"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="reset" value="重置">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
<center>
</body>
</html>
doAdd.php
<html>
<head>
<meta charset="utf-8">
<title>我的留言板</title> <?php //用于接受并处理留言将其存放进入记事本中,并提示留言成功 ?>
</head>
<body>
<center>
<h3>添加留言</h3>
<?php include("menu.php");?>
<?php include("link.php");?>
<?php
$title=$_POST["title"];
$author=$_POST["author"];
$content=$_POST["content"];
$time=time();
$ip=$_SERVER["REMOTE_ADDR"];
$sql="insert into table1 (title,author,content,time,ip) values ('$title','$author','$content','$time','$ip')";
$res=mysqli_query($link,$sql);
mysqli_close($link);
$alert="alert('success!')";
echo "<script>".$alert."</script>";
?>
</center>
</body>
</html>
show.php
<html>
<head>
<meta charset="utf-8">
<title>我的留言板</title>
</head>
<body>
<center>
<?php include("menu.php");?>
<?php include("link.php");?>
<h3>查看留言</h3>
<table border="1">
<tr>
<th>标题</th>
<th>作者</th>
<th>内容</th>
<th>时间</th>
<th>IP</th>
<th>操作</th>
</tr>
<?php
error_reporting(0);
$info=mysqli_query($link,"select * from table1"); //info即结果集
while($list=mysqli_fetch_assoc($info)){
echo "<tr>";
echo "<td align='left'>$list[title]</td>";
echo "<td align='left'>$list[author]</td>";
echo "<td align='left'>$list[content]</td>";
echo "<td>".date("Y-m-d H:i:s",$list[time]+8*3600)."</td>";
echo "<td align='left'>$list[ip]</td>";
echo "<td><a href='del.php?id={$list[id]}'>删除</a></td>";
echo "</tr>";
}
?>
</table>
</center>
</body>
</html>
del.php
<html>
<head >
<meta charset="utf-8">
<title>我的留言板</title>
</head>
<body>
<center>
<?php include("menu.php"); ?>
<?php include("link.php"); ?>
<h3>删除留言</h3>
<?php
$id=$_GET["id"];
mysqli_query($link,"delete from table1 where id='$id'");
$alert="alert('success!')";
echo "<script>".$alert."</script>";
?>
</center>
</body>
</html>
link.php
<?php
$link=mysqli_connect("localhost","root","6464");
mysqli_query($link,"create database liuyan character set utf8 ");
$sql=create table table1(
title varchar(30),
author varchar(10),
content varchar(100),
time varchar(30),
ip varchar(10),
id smallint unsigned auto_increment
)charset utf8 engine innodb;
mysqli_query($link,$sql);
?>
menu.php
<h1 color="red">我的留言板</h1>
<a href="index.php">添加留言</a>
<a href="show.php">查看留言</a>
<hr width="100"/>
index.php
<html>
<head>
<meta charset="utf-8">
<title>我的留言板</title> <?php //初始页面,用于收集留言并发送到doADD.php里?>
</head>
<body>
<center>
<?php include("menu.php");?>
<?php include("link.php");?>
<form action="doAdd.php" method="post">
<table width="400">
<h3>添加留言</h3>
<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="3" cols="17" name="content"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="reset" value="重置">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
<center>
</body>
</html>
doAdd.php
<html>
<head>
<meta charset="utf-8">
<title>我的留言板</title> <?php //用于接受并处理留言将其存放进入记事本中,并提示留言成功 ?>
</head>
<body>
<center>
<h3>添加留言</h3>
<?php include("menu.php");?>
<?php include("link.php");?>
<?php
$title=$_POST["title"];
$author=$_POST["author"];
$content=$_POST["content"];
$time=time();
$ip=$_SERVER["REMOTE_ADDR"];
$sql="insert into table1 (title,author,content,time,ip) values ('$title','$author','$content','$time','$ip')";
$res=mysqli_query($link,$sql);
mysqli_close($link);
$alert="alert('success!')";
echo "<script>".$alert."</script>";
?>
</center>
</body>
</html>
show.php
<html>
<head>
<meta charset="utf-8">
<title>我的留言板</title>
</head>
<body>
<center>
<?php include("menu.php");?>
<?php include("link.php");?>
<h3>查看留言</h3>
<table border="1">
<tr>
<th>标题</th>
<th>作者</th>
<th>内容</th>
<th>时间</th>
<th>IP</th>
<th>操作</th>
</tr>
<?php
error_reporting(0);
$info=mysqli_query($link,"select * from table1"); //info即结果集
while($list=mysqli_fetch_assoc($info)){
echo "<tr>";
echo "<td align='left'>$list[title]</td>";
echo "<td align='left'>$list[author]</td>";
echo "<td align='left'>$list[content]</td>";
echo "<td>".date("Y-m-d H:i:s",$list[time]+8*3600)."</td>";
echo "<td align='left'>$list[ip]</td>";
echo "<td><a href='del.php?id={$list[id]}'>删除</a></td>";
echo "</tr>";
}
?>
</table>
</center>
</body>
</html>
del.php
<html>
<head >
<meta charset="utf-8">
<title>我的留言板</title>
</head>
<body>
<center>
<?php include("menu.php"); ?>
<?php include("link.php"); ?>
<h3>删除留言</h3>
<?php
$id=$_GET["id"];
mysqli_query($link,"delete from table1 where id='$id'");
$alert="alert('success!')";
echo "<script>".$alert."</script>";
?>
</center>
</body>
</html>
相关文章推荐
- 一个关于if else容易迷惑的问题
- Android之获取手机上的图片和视频缩略图thumbnails
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 数据库链接字符串查询网站
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- DB2实例管理
- DB2实例管理
- Php Installing An Expansion
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 第三章 数据库备份和还原
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法