前几天做到一些CMS的题,涉及一些sql的代码审计,于是尝试着自己开发一个连接数据库的博客,加深一遍理解
简单实现了登录,验证,登出,目录,增删改查等功能
下面贴代码
conn.php
<?phpsession_start();$username = "root";
$password = "root";$port = 3306;
$host = "localhost";
$db = "blog";$conn = new mysqli($host,$username,$password,$db,$port);if($conn->connect_error){die("数据库连接失败".$conn->connect_error);
}
check.php
<?phpif($_SESSION['login']!=1){header("location:login.html");
}
index.php
<?phpheader("location:main.php");?>
index.html
<!DOCTYPE html>
<html><head><title>博客系统登陆</title><meta charset="utf-8"></head><body><center><br><h1>博客系统管理员登陆</h1><hr><form action="login.php" enctype="application/x-www-form-urlencoded" method="post">账号<input type="text" name="username"> <br> <br>密码<input type="password" name="password"> <br> <br><input type="submit" value="登陆"></form></center></body>
</html>
login.php
<?phprequire "conn.php";$username = $_POST['username'];
$password = $_POST['password'];$sql = "select * from user where username = '$username' and password = '$password'";$result = $conn->query($sql);if(!$result){die("查询失败");
}if($row=$result->fetch_array()){$_SESSION['login']=1;echo "登陆成功,欢迎你".$row['username'];
}else{echo "登陆失败";
}$conn->close();?><!DOCTYPE html>
<html><head><title>博客首页</title><meta charset="utf-8"></head><body><center><br><h1>博客首页</h1><hr><a href="main.php">首页</a>|<a href="page_list.php">文章列表</a>|<a href="page_add.php">新增文章</a></center></body>
</html>
logout.php
<?phpsession_start();$_SESSION['login']=0;session_destroy();header("location:main.php");
main.php
<!DOCTYPE html>
<html><head><title>博客首页</title><meta charset="utf-8"></head><body><center><br><h1>博客首页</h1><hr><a href="main.php">首页</a>|<a href="page_list.php">文章列表</a>|<a href="page_add.php">新增文章</a></center></body>
</html>
page_add.php
<?phprequire "conn.php";
require "check.php";$title=$_POST['title'];
$content=$_POST['content'];if(isset($title) && isset($content)){$sql = "insert into page (title,content) values('$title','$content')";$result = $conn->query($sql);if($result){echo "文章新增成功";}else{echo "文章新增失败".$conn->error;}
}?><!DOCTYPE html>
<html><head><title>新增文章</title><meta charset="utf-8"></head><body><center><br><h1>新增文章</h1><a href="main.php">首页</a>|<a href="page_list.php">文章列表</a>|<a href="page_add.php">新增文章</a><a href="logout.php">退出系统</a><hr><form action="page_add.php" enctype="application/x-www-form-urlencoded" method="post">标题<input type="text" name="title"> <br> <br>内容<input type="text" name="content"> <br> <br><input type="submit" value="录入文章"></form></center></body>
</html>
page_delete.php
<?phprequire "conn.php";
require "check.php";$id=$_GET['id'];if(isset($id) ){$sql = "delete from page where id =$id";$result = $conn->query($sql);if($result){echo "文章删除成功";}else{echo "文章删除失败".$conn->error;}
}$conn->close();?><!DOCTYPE html>
<html><head><title>博客首页</title><meta charset="utf-8"></head><body><center><br><h1>博客首页</h1><hr><a href="main.php">首页</a>|<a href="page_list.php">文章列表</a>|<a href="page_add.php">新增文章</a><a href="logout.php">退出系统</a></center></body>
</html>
page_detail.php
<?phprequire "conn.php";$id= $_GET['id'];$id=$id?$id:1;$sql = "select id,title,content from page where id = '$id'";
echo $sql;$result = $conn->query($sql);$page = $result->fetch_array();?><!DOCTYPE html>
<html><head><title>我的博客</title><meta charset="utf-8"></head><body><center><br><h1><?=$page['title'];?></h1><a href="main.php">首页</a>|<a href="page_list.php">文章列表</a>|<a href="page_add.php">新增文章</a><hr><p><?=$page['content'];?></p></center></body>
</html><?php$conn->close();
?>
page_ edit.php
<?phprequire "conn.php";
require "check.php";$title=$_POST['title'];
$content=$_POST['content'];
$id=$_POST['id'];if(isset($title) && isset($content)){$sql = "update page set title = '$title',content = '$content' where id =$id";$result = $conn->query($sql);if($result){echo "文章修改成功";}else{echo "文章修改失败".$conn->error;}
}else{$id= $_GET['id'];$id=$id?$id:1;$sql = "select id,title,content from page where id = $id";$result = $conn->query($sql);$page = $result->fetch_array();
}?><!DOCTYPE html>
<html><head><title>修改文章</title><meta charset="utf-8"></head><body><center><br><h1>修改文章</h1><a href="main.php">首页</a>|<a href="page_list.php">文章列表</a>|<a href="page_add.php">新增文章</a><a href="logout.php">退出系统</a><hr><form action="page_edit.php" enctype="application/x-www-form-urlencoded" method="post"><input type="hidden" name="id" value="<?=$page['id'];?>">标题<input type="text" name="title" value="<?=$page['title'];?>"> <br> <br>内容<input type="text" name="content" value="<?=$page['content'];?>"> <br> <br><input type="submit" value="录入文章"></form></center></body>
</html><?php$conn->close();
?>
page_list.php
<?phprequire "conn.php";$sql = "select id,title from page";$result = $conn->query($sql);$list = $result->fetch_all(MYSQLI_ASSOC);?><!DOCTYPE html>
<html><head><title>我的博客</title><meta charset="utf-8"></head><body><center><br><h1>文章列表</h1><a href="main.php">首页</a>|<a href="page_list.php">文章列表</a>|<a href="page_add.php">新增文章</a><hr><ul><?phpforeach($list as $p){?><li><a href="page_detail.php?id=<?=$p['id'];?>"><?=$p['title'];?>| <a href="page_detail.php?id=<?=$p['id'];?>">查看<a>| <a href="page_edit.php?id=<?=$p['id'];?>">修改<a>| <a href="page_delete.php?id=<?=$p['id'];?>">删除<a></li><?php}?></ul></center></body>
</html><?php$conn->close();
?>
总结:
什么是sql
sql是一门语言,通过sql语句可以快速实现数据的增删改查
Create 增
Delete 删
Update 改
Read 查
CURD 就是指对数据的增删改查
什么是数据库
关系型数据库
非关系型数据库
关系型数据库
把所有的数据变为表格存放
常见有
Oracle
Mysql/MariaDB
SQLServer
Access
Sqlite
非关系型数据库
nosql数据库
sns 社交软件 web2.0 所有内容 由用户产生 并 由用户消费
Membase
MongoDB
Mysql/Mariadb 为主
博客系统代码
sql语句
select id,username,password from user;
update page set title='aaa',content='bbb' where id =1;
delete from page where id =1;
insert into page (title,content) values('aaa','bbb');
php语句
$conn = new mysqli($host,$username,$password,$db,$port);
$conn->query();
$result->fetch_all();
$result->fetch_array();