您的位置:首页 > 数据库

简易留言板(数据库交互)

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 php 留言板