PHP+MySQL实现留言板功能
先放效果图这个UI界面是刚开始写的,丑是丑了点,凑合看吧(话说我最新写的那一版UI还是很好看的)把注意力放在核心功能上。首先,我们留言板需要从数据库中读取一些数据。我们可以先用MySQL 命令 写进去一些东西,方便debug。要读取,首先需要链接数据库$con = mysqli_connect("localhost","root","root"); //三个参数分别是端口用户名和密码mysqli_
·
先放效果图
这个UI界面是刚开始写的,丑是丑了点,凑合看吧(话说我最新写的那一版UI还是很好看的)
把注意力放在核心功能上。
首先,我们留言板需要从数据库中读取一些数据。我们可以先用MySQL 命令 写进去一些东西,方便debug。要读取,首先需要链接数据库
$con = mysqli_connect("localhost","root","root"); //三个参数分别是端口用户名和密码
mysqli_select_db($con,"text");//选择数据库
这样我们就可以链接到数据库了。然后就需要开始进行读取操作了。
$result=mysqli_query($con,"select * from user");
while($row = mysqli_fetch_array($result))
{
echo "<div class='line'>";
echo $row['name']." ".$row['pulldate'];
echo "<br /><p class='textrow'>"; //当然你需要自己写相应的css样式
echo $row['text'];
echo "</p>";
echo "</div>";
}
这样就可以读取并且输出了。
然后我们还需要一个删除操作,当然一般我们都是按照时间戳或者其他的unique内容作为删除的标准,这里为了方便我使用的是以内容为参照
$con = mysqli_connect("localhost","root","root");
mysqli_select_db($con,"text");
$result=mysqli_query($con,"select * from user");
$text=$_GET['id'];
echo $text;
$sql="delete from user where text ='".$text."'";
if(mysqli_query($con,$sql))
{
echo "删除成功";
}
当然,为了防止任何人都可以删除我们的数据,我们需要一个管理员权限。这里我使用了session来实现跨页面传值
<?php
session_start();
$_SESSION['flag']=0;
$con = mysqli_connect("localhost","root","root");
mysqli_select_db($con,"text");
$result=mysqli_query($con,"select * from admin");
$row = mysqli_fetch_array($result);
if($row['user']==$_POST['account']&&$row['pastword']==$_POST['word'])
{
echo "密码正确";
$_SESSION['flag']=1;
}
else
echo "密码错误";
?>
最后我再把完整的代码给出大家吧
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" />
<meta content="text/html" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<meta name="referrer" content="no-referrer">
<link rel="stylesheet" type="text/css" href="mainstyle.css" />
</head>
<body>
<?php
session_start();
?>
<nav style="height: 1.875rem; margin-bottom: 8px;background-color: #FFFFFF;margin-bottom: 0.625rem;
border-bottom: 4px solid #00000010;box-shadow: 0px 10px 5px #888888;">
<p class="rtext">
<a href="sign_in.html" class="tech">管理员登陆</a>
</p>
</nav>
<br/>
<?php
$con = mysqli_connect("localhost","root","root");
mysqli_select_db($con,"text");
$result=mysqli_query($con,"select * from user");
?>
<div style="background-image: linear-gradient(#DFDFDF,#e0c1ff);width: 80%;position: relative;left: 10%;padding-top: 0.3rem;">
<?php
while($row = mysqli_fetch_array($result))
{
echo "<div class='line'>";
echo $row['name']." ".$row['pulldate'];
echo "<br /><p class='textrow'>";
echo $row['text'];
echo "</p>";
$inf="<a href='./delete.php?id=".$row['text']."'>删除</a>";
echo $inf;
/*echo "<form action='delete.php' method='post'>
<input type='submit' value='删除'>
</form>
";*/
echo "</div>";
}
?>
<div class="from">
<form style="position: relative;left: 0.5rem;" action="insert.php" method="post">
<br>
ID:
<input type="text" name="name"><br>
<textarea style="width: 60%;height: 4rem;border-radius: 0.25rem;border: gray 0.03125rem solid;margin: 0.3125rem;"
type="text" name="txt"></textarea>
<input style="height: 3.5rem;width: 3.5rem;color: #FFFFFF;background-color: #00BFFF;border-radius: 0.3125rem;
border: none;position: relative;top: -2.5rem;white-space: normal;font-size: medium;"
type="submit" value="发表 评论">
</form>
</div>
<?php
echo "状态:".$_SESSION['flag'];
?>
</div>
</body>
</html>
sign_in.html
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" />
<meta content="text/html" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<meta name="referrer" content="no-referrer">
<style type="text/css">
.mainlog{
color: white;
width:25rem;
position: relative;
left: 40%;
background-color:#bd1c21;
height:200px;
text-align: center;
padding: 0.9375rem;
background:
linear-gradient(-60deg,transparent 100px, #bd1c21 0)bottom right,
linear-gradient(-120deg,transparent 100px, #bd1c21 0)top right,
linear-gradient(120deg,transparent 100px, #bd1c21 0)top left,
linear-gradient(60deg,transparent 100px, #bd1c21 0)bottom left;
background-size: 50% 50%;
background-repeat: no-repeat;
}
}
html,body{
height: 100%;
padding: 0px;
margin: 0px;
}
body{
padding: 0rem;
margin: 0rem;
background-repeat: no-repeat;
background-size: cover;
}
.dim{
transition:1s;
background-color: rgba(0, 0, 0, 0.5);
height: 100%;
padding: 0rem;
margin: 0rem;
}
input.in{
width: 15rem;
height: 1.7rem;
font-size: 1rem;
border: 0.15625rem solid whitesmoke;
border-radius: 0.3125rem;
}
h1{
margin: 6.25rem;
font-size: 12.5rem;
}
</style>
</head>
<body background="background.png">
<div id="dimdiv">
<br />
<br />
<br />
<br />
<div class="mainlog" id="log_in">
登錄
<br />
<br />
<form action="ensure.php" method="post">
<input class="in" type="text" name="account" onclick="dim()">
<br>
<br />
<input class="in" type="password" name="word" onclick="privite()">
<br>
<br />
<input style="color: white;background-color: #BD1C21;" type="submit" value="sign in">
</form>
</div>
</div>
<script>
let count=0;
function privite()
{
if(count===0){
let log=document.getElementById("log_in");
log.style="transition:1s;transform:translateY(350px)"
let dive=document.getElementById("dimdiv");
dive.style="background-color:rgba(0, 0, 0, 0.9)";
let jimi = document.createElement("h1");
jimi.style="color: #BD1C21;text-align:center;";
jimi.innerHTML="極 秘";
document.getElementById("dimdiv").appendChild(jimi);
count=1;
}
}
function dim(){
let dive=document.getElementById("dimdiv");
dive.className="dim";
}
</script>
</body>
</html>
delete.php
<?php
session_start();
if(!$_SESSION['flag'])
{
echo "<script>
alert('请登录');
window.location.href = 'text.php';
</script>";
}
else{
$con = mysqli_connect("localhost","root","root");
mysqli_select_db($con,"text");
$result=mysqli_query($con,"select * from user");
$text=$_GET['id'];
echo $text;
$sql="delete from user where text ='".$text."'";
if(mysqli_query($con,$sql))
{
echo "删除成功";
}
}
?>
ensure.php
<script type="text/javascript" src="main.js"></script>
<?php
session_start();
$_SESSION['flag']=0;
$con = mysqli_connect("localhost","root","root");
mysqli_select_db($con,"text");
$result=mysqli_query($con,"select * from admin");
$row = mysqli_fetch_array($result);
if($row['user']==$_POST['account']&&$row['pastword']==$_POST['word'])
{
echo "密码正确";
$_SESSION['flag']=1;
}
else
echo "密码错误";
?>
<script>
setTimeout(function(){window.location.href='index.php';},1000);
</script>
insert.php
<?php
$con = mysqli_connect("localhost","root","root");
mysqli_select_db($con,"text");
echo $_POST["name"];
echo "<br>";
echo $_POST["txt"];
$time=date('Y-m-d H:i:s');
$sql="INSERT INTO user VALUES ('".$_POST["name"]."','".$_POST["txt"]."','".$time."')";
echo $sql;
if(mysqli_query($con,$sql))
echo "发布成功";
?>
<script>
window.location="text.php";
alert("发布成功");
</script>
更多推荐
已为社区贡献3条内容
所有评论(0)