留言板网页
源码:
(1)db.php
<?php
//连接数据库
$mysqli=new mysqli("localhost","root","","web");
if($mysqli->connect_errno>0){
echo"连接错误";
echo $mysqli->connect_error;
exit;
}
$mysqli->query("SET NAMES UTF8");//处理字符乱码的问题
?>
(2)delete.php
<?php
session_start();
if(isset($_SESSION['username'])==false){
echo "需要管理员登录";
exit;
}
include("input_class.php");
$input=new input();
include("db.php");
$id=$input->get("id");
$sql="DELETE from msg WHERE id='{$id}'";
$is=$mysqli->query($sql);
if($is==true){
header("Location:index.php");
}else{
echo"删除失败";
}
(3)index.php
<?php
session_start();
include("db.php");
//从数据库取数据
$sql="SELECT * FROM msg";
$mysqli_result=$mysqli->query($sql);
$rows=array();
while($row=$mysqli_result->fetch_array(MYSQLI_ASSOC)){
$rows[]=$row;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>留言板</title>
<link rel="stylesheet" href="style.css">
<style type="text/css">
</style>
</head>
<body>
<div class="add">
<form action="save.php" method="post">
<textarea name="msg">留言内容</textarea>
<input class="user" name="user" type="text">
<input class="btn" type="submit" value="发表">
<a class="btn" href="login.php">登录</a>
</form>
</div>
<div class="msg">
<?php
foreach($rows as $row){
$t=date("Y-m-d H:i:s",$row['intime']);
?>
<div class="item">
<span class="user"><?php echo $row['username'];?></span>
<span class="time">
<?php echo $t; ?>
<?php
if(isset($_SESSION['username'])){
?>
<a οnclick='return confirm("你确定要删除吗?")'; href="delete.php?id=<?php echo $row['id'];?>">删除</a>
<?php
}
?>
</span>
<p>
<?php
echo $row['content'];
?>
</p>
</div>
<?php
}
?>
</div>
</body>
</html>
(4)input_class.php
<?php
class input{
function post($key){
if(isset($_POST[$key])==false){
return false;
}
$val=$_POST[$key];
//代码的恶意过滤
//代码的黄赌毒检查
return $val;
}
function get($key){
if(isset($_GET[$key])==false){
return false;
}
$val=$_GET[$key];
//代码的恶意过滤
//代码的黄赌毒检查
return $val;
}
}
?>
(5)login.php
<?php
session_start();
include("input_class.php");
$input=new input();
include("db.php");
$act=$input->get('act');
if($act!=false){
$username=$input->post('username');
$password=$input->post('password');
$sql="SELECT * from admin WHERE username='{$username}'and password='{$password}'";
$mysqli_result=$mysqli->query($sql);
if($row=$mysqli_result->fetch_array()){
$_SESSION['username']=$row['username'];
echo "登录成功";
header("Location:index.php");
}else{
echo "登录失败";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>管理员登录</title>
</head>
<body>
<form action="login.php?act=chk" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
(6)save.php
<?php
include("input_class.php");
include("db.php");
//处理用户传来的信息和用户名
$input=new input();
$msg=$input->post('msg');
$user=$input->post('user');
$t=time();
if ($msg=='') {
echo "留言内容不能为空";
exit;
}
if ($user=='') {
echo "用户名不能为空";
exit;
}
//插入数据库中
$sql="INSERT INTO msg(`username`,`content`,`intime`) values('$user','$msg','$t')";
$is=$mysqli->query($sql);
if($is){
echo "插入成功";
header("Location:index.php");
}else{
echo "插入失败";
}
?>
(7)style.css
.add{
margin:0px auto;
overflow: hidden;
width: 600px;
}
.add textarea{
width: 99%;
height: 100px;
}
.add .user{
float: left;
}
.add .btn{
float: right;
}
.msg{
width: 600px;
margin: 20px auto;
overflow: hidden;
}
.item{
border:solid 1px #CCC;
margin: 10px 0;
}
.item.user{
float: left;
}
.item .time{
float: right;
}
更多推荐
简单的动态网页设计及开发(关联数据库)
发布评论