当前位置: 首页 > article >正文

【SQL】简单博客开发代码

前几天做到一些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.kler.cn/a/133075.html

相关文章:

  • hrnet人体关键点检测模型适配atlas笔记
  • nginx反向代理上游服务器主从配置
  • java版嘎嘎快充汽车单车充电系统源码系统jeecgboot
  • 第八节 如何结合AAA实现用户远程登录-路由基础
  • Essential Cell Biology--Fifth Edition--Chapter one (6)
  • 如何保证MySQL与Redis缓存的数据一致性?
  • 客户端性能优化实践
  • 特征缩放和转换以及自定义Transformers(Machine Learning 研习之九)
  • 飞书开发学习笔记(八)-开发飞书小程序Demo
  • 牛客 —— 链表中倒数第k个结点(C语言,快慢指针,配图)
  • Linux网络——HTTP
  • Swift 如何打造兼容新老系统的字符串分割(split)方法
  • <C++>类和对象下|初始化列表|explicit static|友元|内部类|匿名对象|构造函数的优化
  • vue-组件通信(二)
  • DHCP协议详解
  • 竞赛选题 疫情数据分析与3D可视化 - python 大数据
  • 机器学习笔记 - Ocr识别中的文本检测EAST网络概述
  • 机器视觉工程师吐槽的常见100个名场面
  • excel导入 Easy Excel
  • Android SdkManager简介
  • 网站优化工具Google Optimize
  • 微机原理_12
  • 比亚迪刀片电池与特斯拉4680电池比较
  • C语言循环结构 for循环
  • 若依框架数据源切换为pg库
  • 2311ddip1000不能从函数返回域引用