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

这篇博客介绍了作者在学习PHP过程中,如何创建一个简易的数据库交互式留言板。通过连接本地MySQL数据库,创建名为'liuyan'的数据库,并建立一个包含标题、作者、内容、时间、IP和自动递增ID的table1表,以实现留言板的基础功能。
摘要由CSDN通过智能技术生成

刚开始学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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值