留言板功能实现(php入门小白)
PS:HTML,CSS,以及留言板功能类型借鉴了大牛的博客资源,留言板具体功能为自己实现。
留言板功能
(1)在主界面显示已添加的留言
(2)添加留言
(3)管理员登录
(4)修改留言
(5)删除留言
具体代码
主界面: index.php
<html>
<head>
<title>欢迎来到小灰灰的留言本吼吼吼</title>
<style type="text/css">
TD {
font-size: 12px;
line-height: 150%;
}
</style>
</head>
<body>
<table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=480 bordercolor=black height=382>
<tr>
<td height=100 bgcolor=#6c6c6c style="font-size:30px;line-height:30px">
<font color=#ffffff face="黑体">欢迎来到小灰灰的留言本吼吼吼</font>
</td>
</tr>
<tr>
<td height=25>
<a href=send.php>[我要写留言]</a>
<a href=login.php>[管理留言]</a>
</td>
</tr>
<tr>
<td height=200>
<?php
$conn = mysqli_connect('localhost', 'username', 'userpassword');
if (!$conn) {
die('connect error' . mysqli_error($conn));
}
$sql = "SELECT * FROM messageboard ORDER BY dateline DESC";
mysqli_select_db($conn, 'test');
$retval = mysqli_query($conn, $sql);
if (!$retval) {
die('use database error!');
} else {
$dataNum = mysqli_num_rows($retval);
$i = 0;
while ($row = mysqli_fetch_assoc($retval)) {
echo '->[' . $row['author'] . '] on ' . $row['dateline'] . ' saied:<br>'
. $row['message'];
if ($dataNum > 1 && (++$i) != $dataNum)
echo '<hr>';
}
}
mysqli_close($conn);
?>
</td>
</tr>
<tr>
<td height=80 bgcolor=#6c6c6c align=center>
<font color="#FFFFFF">
版权所有:<a href="https://gitee.com/hui_net/LearnRecords">小灰灰的码云</a><br>
E-mail:neuq-xjh@foxmail.com
</font>
</td>
</tr>
</table>
</body>
</html>
留言界面: send.php
<!-- 1.用户填写留言部分 send.php -->
<!-- 可以首先编写send页面,只有用户提交了留言才能进行后面的留言显示,留言管理等等 -->
<html>
<head>
<title>欢迎来到小灰灰的留言本吼吼吼</title>
</head>
<body>
<!-- border-collapse:collapse合并表格的边框 -->
<table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=600 bordercolor=black>
<tr>
<td height=100 bgcolor=#6c6c6c>
<font style="font-size:30px" color=#ffffff face="黑体">欢迎来到小灰灰的留言本吼吼吼</font>
</td>
</tr>
<tr>
<td height=30>
<a href=send.php>[我要写留言]</a>
<a href=login.php>[管理留言]</a>
</td>
</tr>
<tr>
<td height=200>
<form method="POST" action="send.php">
<table border="1" width="95%" id="table1" cellspacing="0" cellpadding="0" bordercolor="#808080" style="border-collapse:collapse" height="265">
<tr>
<td colspan="2" height="30">
<p align="center">欢迎填写你的留言</p>
</td>
</tr>
<tr>
<td width="40%">
<p align="left">你的名字</p>
</td>
<td width="67%">
<input type="text" name="name" size="25">
</td>
</tr>
<tr>
<td width="40%">
<p align="left">留言内容</p>
</td>
<td width="67%">
<textarea rows="10" name="content" cols="58"></textarea>
</td>
</tr>
<tr>
<td width="99%" colspan="4">
<p align="center">
<input type="submit" value="提交" name="B1">
</p>
</td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td height=80 bgcolor=#6c6c6c align=center>
<font color="#FFFFFF">
版权所有:<a href="https://gitee.com/hui_net/LearnRecords">小灰灰的码云</a><br>
E-mail:neuq-xjh@foxmail.com
</font>
</td>
</tr>
</table>
</body>
</html>
<?php
$name = $_POST["name"];
if ($name != null) {
$content = $_POST["content"];
$addtime = date("Y-m-d h:i:s");
$link = mysqli_connect("localhost", "username", "userpassword");
if ($link)
echo "connect success!<br>";
else {
echo "connect error!<br>";
}
mysqli_select_db($link, "test");
$sql = "INSERT INTO messageboard(author,dateline,message,response) VALUES('$name','$addtime','$content','')";
mysqli_query($link, $sql);
mysqli_close($link);
echo "<script>
alert('留言成功!单击确定查看留言.');
location.href='index.php';
</script>";
}
?>
登录界面: login.php
<!-- 3.管理员登录页面 login.php -->
<!-- 供管理员登录 -->
<!-- 体会session实现用户登录的方法 -->
<html>
<head>
<title>欢迎来到小灰灰的留言本吼吼吼</title>
</heda>
<body>
<table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=400 bordercolor=black height="358">
<tr>
<td height=100 bgcolor=#6c6c6c style="font-size:30px;line-height:30px">
<font color=#ffffff face="黑体">欢迎来到小灰灰的留言本吼吼吼</font>
</td>
</tr>
<tr>
<td height=25>
<a href=send.php>[我要写留言]</a>
<a href=login.php>[管理留言]</a>
</td>
</tr>
<tr>
<td height=178>
<form method="POST" action="login.php">
<table border="1" width="95%" id="table1" cellspcing="0" cellpadding="0" bordercolor="#808080" style="border-collapse" height="154">
<tr>
<td colspan="2" height="29">
<p align="center">欢迎管理员登录</p>
</td>
</tr>
<tr>
<td width="32%">
<p align="center">用户名</P>
</td>
<td width="67%">
<input type="text" name="name" size="20">
</td>
</tr>
<tr>
<td width="32%">
<p align="center">密 码</p>
</td>
<td>
<input type="password" name="password" size="20">
</td>
</tr>
<tr>
<td width="99%" colspan="2">
<p align="center"><input type="submit" value="登录" name="B1"></p>
</td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td height=80 bgcolor=#6c6c6c align=center>
<font color="#FFFFFF">
版权所有:<a href="https://gitee.com/hui_net/LearnRecords">小灰灰的码云</a><br>
E-mail:neuq-xjh@Foxmail.com
</font>
</td>
</tr>
</table>
</body>
</html>
<?php
$guestName = $_POST['name'];
$guestPass = $_POST['password'];
if($guestName!=null && $guestPass!=null){
$link = mysqli_connect('localhost','username','userpassword');
if(!$link){
die('connect error'.mysqli_error($link));
}
mysqli_select_db($link,'test');
$sql = "SELECT * FROM admin WHERE username='$guestName'";
$retval = mysqli_query($link,$sql);
if(mysqli_num_rows($retval) > 0){
$info = mysqli_fetch_array($retval);
if($info['userpass']==$guestPass){
echo "<script>
alert('log success!');
location.href='manage.php';
</script>";
}else{
echo "<script>
alert('log fail!');
</script>";
}
}else{
echo "<script>
alert('guest not exist!');
</script>";
}
}
?>
管理界面: manage.php
<!DOCTYPE HTML>
<HTML>
<Head>
<meta http-equiv="CONTENT-TYPE" ; content="text/html" ; charset="UTF-8">
<title>留言列表</title>
<style type="text/css"> </style>
</Head>
<body>
<div>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Time</th>
<th class="content">Content</th>
<th>操作</th>
</tr>
<?php
$conn = mysqli_connect('localhost', 'username', 'userpassword');
if (!$conn) {
die('connect error' . mysqli_error($conn));
}
mysqli_select_db($conn, 'test');
$sql = 'SELECT * FROM messageboard';
$retval = mysqli_query($conn, $sql);
if ($retval) {
while ($v = mysqli_fetch_assoc($retval)) { ?>
<tr>
<td><?php echo $v['id']; ?></td>
<td><?php echo $v['author']; ?></td>
<td><?php echo $v['dateline']; ?></td>
<td><?php echo $v['message']; ?></td>
<td>
<a href="changeContent.php?id=<?= $v['id'] ?>">修改</a>
<a href="delete.php?id=<?= $v['id'] ?>">删除</a>
</td>
</tr>
<?php }
} else {
echo 'mysqli_query() error!';
} ?>
<table>
</div>
</body>
<a href="index.php"><------返回主界面------></a>
</HTML>
修改界面: changeContent.php
<?php
$id = $_GET['id'];
$link = mysqli_connect('localhost', 'username', 'userpassword');
mysqli_select_db($link,'test');
$sql = "SELECT * FROM messageboard WHERE id=" . $id;
$retval = mysqli_query($link, $sql);
if ($retval) {
while ($row = mysqli_fetch_assoc($retval)) { ?>
<form method="POST" action="update.php" >
<input type="hidden" name="id" value="<?= $row['id'] ?>">
你的名字:<INPUT TYPE="text" NAME="name" value="<?= $row['author'] ?>" /><br />
留言内容:<TEXTAREA NAME="content" ROWS="8" COLS="30"><?= $row['message'] ?></TEXTAREA><br />
<INPUT TYPE="submit" value='提交修改'/>
</FORM>
<?php }
}
?>
update.php
<?php
$name = $_POST['name'];
$content = $_POST['content'];
$time = date("Y-m-d h:i:s");
$link = mysqli_connect('localhost', 'username', 'userpassword');
if (!$link) {
die('connect error' . mysqli_error($link));
}
mysqli_select_db($link, 'test');
$sql = 'UPDATE messageboard SET author="' . $name . '",message="' . $content . '",dateline="' . $time . '" WHERE id="' . $_POST['id'] . '"';
$retval = mysqli_query($link, $sql);
if ($retval) {
echo '修改成功,3秒后自动跳转......'; ?>
<meta http-equiv="refresh" content="3;url=manage.php">
<?php
} else {
echo '修改失败,3秒后自动跳转......'; ?>
<meta http-equiv="refresh" content="3;url=manage.php">
<?php
}
?>
删除界面: delete.php
<html>
<?php
$link = mysqli_connect('localhost', 'username', 'userpassword');
if (!$link) {
die('connect error' . mysqli_error($link));
}
mysqli_select_db($link, 'test');
$id = $_GET['id'];
$sql = 'DELETE FROM messageboard WHERE id='.$id;
$retval = mysqli_query($link, $sql);
if ($retval) {
echo '删除成功,3秒后自动跳转......'; ?>
<meta http-equiv="refresh" content="3;url=manage.php">
<?php
} else {
echo '删除失败,3秒后自动跳转......';
}
?>
</html>