1.创建留言表准备阶段
主机:centos7
开启apache和mariadb服务
systemctl start httpd //开启apache
systemctl start mariadb //开启mariadb服务
mysql -uroot -p123456 //登录数据库
show databases; //查看数据库
create database menu; // 创建名为menu的数据库
use menu //使用menu数据库
create table guestbook(id int(10) auto_increment primary key,txtName varchar(100),txtMessage varchar(500),addtime int(10) default 0) DEFAULT CHARSET=utf8; //创建数据表
2.创建一个静态的留言板
cd /var/www/html && vim html_create.php //在该目录下创建一个php文本
写入代码
<html>
2 <head>
3 <meta charset="utf-8"/>
4 <title>留言板</title>
5 </head>
6 <body>
7 <table width="80%" align="center" border="1" style="margin-bottom:10px;" rules="all" >
8 <tr>
9 <td>标题</td>
10 </tr>
11 <tr>
12 <td>留言内容</td>
13 </tr>
14 </table>
15 <table width="80%" align="center" border="1" style="margin-bottom:10px;" rules="all" >
16 <tr>
17 <td>标题</td>
18 </tr>
19 <tr>
20 <td>留言内容</td>
21 </tr>
22 </table>
23 <table width="80%" align="center" border="1" style="margin-bottom:10px;" rules="all" >
24 <tr>
25 <td>标题</td>
26 </tr>
27 <tr>
28 <td>留言内容</td>
29 </tr>
30 </table>
31 <table width="80%" align="center" border="1" rules="all">
32 <form action="" method="POST">
33 <tr>
34 <td>标题</td>
35 <td><input type="text" name="txtName" value=""/></td>
36 </tr>
37 <tr>
38 <td>留言内容</td>
39 <td><textarea name="txtMessage" cols="100" rows="6"></textarea></td>
40 </tr>
41 <tr>
42 <td></td>
43 <td><input type="submit" name="submit" value="提交" /></td>
44 </tr>
45 </form>
46 </table>
47 </body>
48 </html>
在浏览器上看看效果:http://192.168.1.109/html_create.php
创建php文件连接数据库vim query_connect.php
//创建php文件连接数据库vim query_connect.php
<?php
$result = mysql_connect('localhost','root','123456');
if(!$result){
echo "数据库连接失败";
}
// else{
// echo"success!!!";
// }
mysql_query("set names 'utf8'");
$select_db = mysql_select_db("menu");
if (!$select_db) {
echo "不能连接到此数据库:\n" . mysql_error();
}
?>
3.实现留言功能的显示
//在html_create.php文件内第32行
<form action="" method="POST">
//修改为
<form action="save_add.php" method="POST">
创建save_add.php文件
//创建save_add.php文件
<?php
include"query_connect.php";
if(!empty($_POST['submit']) ){
$txtName = $_POST['txtName'];
$txtMessage = $_POST['txtMessage'];
$sql = "insert into guestbook(txtName,txtMessage,addtime) values('".$txtName."','".$txtMessage."',".time().")";
$result = mysql_query($sql);
if($result){
echo "<script>alert('留言成功!');location.href='html_create.php';</script>";
}else{
echo "<script>alert('留言失败!请重试');location.href='html_create.php';</script>";
}
}
?>
在html_create.php文件头部加入
<?php
include”query_connect.php”;
?>
修改第10-33行的代码为
<?php
$sql = "select id,txtName,txtMessage,addtime from guestbook order by addtime desc";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
?>
<table width="80%" align="center" border="1" style="margin-bottom:10px;" rules="all">
<tr>
<td><?php echo $row['txtName']; ?> <?php echo date('Y-m-d H:i:s',$row['addtime']); ?></td>
</tr>
<tr>
<td><?php echo $row['txtMessage']; ?></td>
</tr>
</table>
<?php } ?>
4.实现留言修改功能
vim html_create.php //编辑文本内容
修改第十八行内容为
<td><?php echo $row['txtName']; ?> <?php echo date('Y-m-d H:i:s',$row['addtime']); ?> [<a href="edit.php?id=<?php echo $row['id'];?>">修改</a>]</td>
创建edit.php写入代码
<?php
include"query_connect.php";
$id = $_GET['id'];
$txtName = "";
$txtMessage = "";
if(isset($id)){
$sql = "select * from guestbook where id=$id";
$result = mysql_query($sql);
$data = mysql_fetch_array($result);
if($data){
$txtName = $data['txtName'];
$txtMessage = $data['txtMessage'];
}
}
?>
<html>
<head>
<meta charset="utf-8"/>
<title>留言板-修改留言</title>
</head>
<body>
<table width="80%" align="center" border="1" rules="all">
<form action="save_add.php" method="POST">
<tr>
<td>标题</td>
<td><input type="text" name="txtName" value="<?php echo $txtName; ?>"/></td>
</tr>
<tr>
<td>留言内容</td>
<td><textarea name="txtMessage" cols="100" rows="6"><?php echo $txtMessage; ?></textarea></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
<td><input type="submit" name="submit" value="提交" /></td>
</tr>
</form>
</table>
</body>
</html>
创建save_add.php文件
<?php
include"query_connect.php";
if(!empty($_POST['submit']) ){
$txtName = $_POST['txtName'];
$txtMessage = $_POST['txtMessage'];
$sql = "insert into guestbook(txtName,txtMessage,addtime) values('".$txtName."','".$txtMessage."',".time().")";
$result = mysql_query($sql);
if($result){
echo "<script>alert('留言成功!');location.href='html_create.php';</script>";
}else{
echo "<script>alert('留言失败!请重试');location.href='html_create.php';</script>";
}
}
?>
5.实现留言的删除
修改html_create.php第18行的内容为
<td><?php echo $row['txtName']; ?> <?php echo date('Y-m-d H:i:s',$row['addtime']); ?> [<a href="edit.php?id=<?php echo $row['id'];?>">修改</a>][<a href="del.php?id=<?php echo $row['id'];?>">删除</a>]</td>
创建del.php写入代码
<?php
include"query_connect.php";
$id=$_GET['id'];
if(isset($id)){
$sql = "delete from guestbook where id=$id";
$result = mysql_query($sql);
if($result){
echo "<script>alert('留言删除成功!');location.href='html_create.php';</script>";
}else{
echo "<script>alert('留言删除失败!');location.href='html_create.php';</script>";
}
}
?>
6.留言表功能截图