简单的动态网页设计及开发(关联数据库)

留言板网页


源码:

(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.org/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;
	}


  • 7
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mind_programmonkey

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值