【SQL】简单博客开发代码

news/2024/5/19 23:40:46 标签: sql, 数据库, php, 安全, 笔记, CTF, WEB

前几天做到一些CMS的题,涉及一些sql的代码审计,于是尝试着自己开发一个连接数据库的博客,加深一遍理解

简单实现了登录,验证,登出,目录,增删改查等功能

下面贴代码

conn.php

<?php

session_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

<?php


if($_SESSION['login']!=1){
    header("location:login.html");
}

 index.php

<?php


header("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 

<?php

require "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

<?php

session_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

<?php


require "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

<?php

require "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

<?php

require "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

<?php

require "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

<?php

require "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>
            <?php

            foreach($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();
 


http://www.niftyadmin.cn/n/5193987.html

相关文章

Java中,ArrayList和LinkedList区别区分,简洁

在Java中&#xff0c;ArrayList和LinkedList都是用来存储和管理数据的类 ArrayList是Java中的一个类&#xff0c;它实现了List接口&#xff0c;是一种基于动态数组实现的集合类常用成员方法&#xff1a; 构造方法&#xff1a;ArrayList() 成员方法&#xff1a;add、remov…

回溯算法(3)--n皇后问题及回溯法相关习题

一、n皇后问题 1、概述 n皇后要求在一个nn的棋盘上放置n个皇后&#xff0c;使得他们彼此不受攻击&#xff0c;皇后可以攻击同一行、同一列、同一斜线上的敌人&#xff0c;所以n皇后问题要求寻找在棋盘上放置这n个皇后的方案&#xff0c;使得任意两个皇后都不在同一行、同一列或…

替换字符串 (前端 js)

在JavaScript中&#xff0c;字符串替换 有多种方法&#xff0c;下面介绍其中一些比较常用的方法。 使用replace()方法、 replace()方法用于在字符串中查…

Python与ArcGIS系列(九)自定义python地理处理工具

目录 0 简述1 创建自定义地理处理工具2 创建python工具箱0 简述 在arcgis中可以进行自定义工具箱,将脚本嵌入到自定义的可交互窗口工具中。本篇将介绍如何利用arcpy实现创建自定义地理处理工具以及创建python工具箱。 1 创建自定义地理处理工具 在arctoolbox中的自定义工具箱…

Typora——优雅的排版也是一种品味

电脑中用于编辑文本的软件&#xff0c;一直以来可谓是层出不穷&#xff0c;大家脑海中一定会浮现出很多名字&#xff1a;word&#xff0c;OneNote&#xff0c;记事本&#xff0c;wps&#xff0c;LaTeX&#xff0c;还有各种小众的office工具&#xff0c;等等等等。今天学长将介绍…

画中画视频剪辑:批量制作画中画视频,让视频更具吸引力和创意

在今天的视频制作环境中&#xff0c;画中画视频剪辑技术已经成为了一种主流。它不仅能增加视频的视觉吸引力&#xff0c;也可以提升观看体验。画中画视频剪辑是一种制作多个视频画面的技术&#xff0c;它可以将两个或更多的视频画面融合在一起&#xff0c;形成一个全新的视频。…

mac系统安装docker desktop

Docker的基本概念 Docker 包括三个基本概念: 镜像&#xff08;Image&#xff09;&#xff1a;相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。比如说nginx,mysql,redis等软件可以做成一个镜像。容器&#…

电子电器架构 —— 车载网关边缘节点路由转发策略

电子电器架构 —— 车载网关边缘节点路由转发策略 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 PS:小细节,本文字数5000+,详细描述了网关在车载框架中的具体性能设置。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 没有人关注你。也无…