IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> 2021-08-16 -> 正文阅读

[PHP知识库]2021-08-16

许愿墙项目

项目图
效果图
连接数据库错误的可以看看我上一篇文章
所需文件
在这里插入图片描述

在这里插入图片描述在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
index.php

<?php
require './common/init.php';
require './common/function.php';

// 获取当前页码
$page = max(input('get', 'page', 'd'), 1);
// 每页显示的条数
$size = 4;

$sql = 'SELECT count(*) FROM `wish`';
if (!$res = mysqli_query($link, $sql)) {
    exit("SQL[$sql]执行失败:" . mysqli_error($link));
}
$total = (int) mysqli_fetch_row($res)[0];

// 查询所有愿望
$sql = 'SELECT `id`,`name`,`content`,`time`,`color` FROM `wish` ORDER BY `id` DESC LIMIT ' . page_sql($page, $size);
if (!$res = mysqli_query($link, $sql)) {
    exit("SQL[$sql]执行失败:" . mysqli_error($link));
}
$data = mysqli_fetch_all($res, MYSQLI_ASSOC);
mysqli_free_result($res);

// 查询结果为空时,自动返回第1页
if (empty($data) && $page > 1) {
    header('Location: ./index.php?page=1');
    exit;
}

// 编辑或删除愿望
$id = max(input('get', 'id', 'd'), 0);
$action = input('get', 'action', 's');
if ($id) {
    $password = input('post', 'password', 's');
    $sql = 'SELECT `name`,`content`,`color`,`password` FROM `wish` WHERE `id`=' . $id;
    if (!$res = mysqli_query($link, $sql)) {
        exit("SQL[$sql]执行失败:" . mysqli_error($link) . $sql);
    }
    if (!$edit = mysqli_fetch_assoc($res)) {
        exit('该愿望不存在!');
    }
    mysqli_free_result($res);
    $checked = isset($_POST['password']) || empty($edit['password']);
    if ($checked && $password !== $edit['password']) {
        $tips = '密码不正确!';
        $checked = false;
    }
    // 删除愿望
    if ($checked && $action == 'delete') {
        $sql = 'DELETE FROM `wish` WHERE `id`=' . $id;
        if (!mysqli_query($link, $sql)) {
            exit('SQL执行失败:' . mysqli_error($link));
        }
        header('Location: ./index.php');
        exit;
    }
}

mysqli_close($link);
require './view/index.html';

save.php

<?php
require './common/init.php';
require './common/function.php';

// 接收变量
$id = max(input('get', 'id', 'd'), 0);
$name = trim(input('post', 'name', 's'));
$color = input('post', 'color', 's');
$content = trim(input('post', 'content', 's'));
$password = input('post', 'password', 's');

// 限制名称最多占用12个字符位置(1个汉字占用2个位置)
$name = mb_strimwidth($name, 0, 12);
// 当名称为空时,使用“匿名”作为默认值
$name = $name ?: '匿名';

// 限制颜色值在合法范围内,使用“green”作为默认值
if (!in_array($color, ['blue', 'yellow', 'green', 'red'])) {
    $color = 'green';
}

// 限制内容长度最多占用80个字符位置
$content = mb_strimwidth($content, 0, 80);

// 限制密码长度最多为6位
$password = (string) substr($password, 0, 6);

// 保存用户的发布时间
$time = time();

// 保存到数据库
if ($id) {
    // 验证密码是否正确
    $sql = 'SELECT `password` FROM `wish` WHERE `id`=' . $id;
    if (!$res = mysqli_query($link, $sql)) {
        exit("SQL[$sql]执行失败:" . mysqli_error($link));
    }
    if (!$data = mysqli_fetch_assoc($res)) {
        exit('该愿望不存在!');
    }
    if ($data['password'] !== $password) {
        exit('密码不正确!');
    }
    $sql = 'UPDATE `wish` SET `name`=?,`color`=?,`content`=? WHERE `id`=?';
    if (!$stmt = mysqli_prepare($link, $sql)) {
        exit("SQL[$sql]预处理失败:" . mysqli_error($link));
    }
    mysqli_stmt_bind_param($stmt, 'sssi', $name, $color, $content, $id);
} else {
    $sql = 'INSERT INTO `wish` (`name`,`color`,`content`,`password`,`time`) VALUES (?,?,?,?,?)';
    if (!$stmt = mysqli_prepare($link, $sql)) {
        exit("SQL[$sql]预处理失败:" . mysqli_error($link));
    }
    mysqli_stmt_bind_param($stmt, 'ssssi', $name, $color, $content, $password, $time);
}
if (!mysqli_stmt_execute($stmt)) {
    exit('数据库操作失败:' . mysqli_stmt_error($stmt));
}
$page = max(input('get', 'page', 'd'), 1);
header("Location: ./index.php?page=$page");

这里是引用
function.php

<?php

/**
 * 接收输入的函数
 * @param array $method 输入的数组(可用字符串get、post来表示)
 * @param string $name 从数组中取出的变量名
 * @param string $type 表示类型的字符串
 * @param mixed $default 变量不存在时使用的默认值
 * @return mixed 返回的结果
 */
function input($method, $name, $type = 's', $default = '')
{
    switch ($method) {
        case 'get': $method = $_GET;
            break;
        case 'post': $method = $_POST;
            break;
    }
    $data = isset($method[$name]) ? $method[$name] : $default;
    switch ($type) {
        case 's': return is_string($data) ? $data : $default;
        case 'd': return (int) $data;
        default: trigger_error('不存在的过滤类型“' . $type . '”');
    }
}

/**
 * 格式化日期
 * @param type $time 给定时间戳
 * @return string 从给定时间到现在经过了多长时间(天/小时/分钟/秒)
 */
function format_date($time)
{
    $diff = time() - $time;
    $format = [86400 => '天', 3600 => '小时', 60 => '分钟', 1 => '秒'];
    foreach ($format as $k => $v) {
        $result = floor($diff / $k);
        if ($result) {
            return $result . $v;
        }
    }
    return '0.5秒';
}

/**
 * 生成分页导航HTML
 * @param string $url 链接地址
 * @param int $total 总记录数
 * @param init $page 当前页码值
 * @param int $size 每页显示的条数
 * @return string 生成的HTML结果
 */
function page_html($url, $total, $page, $size)
{
    // 计算总页数
    $maxpage = max(ceil($total / $size), 1);
    // 如果不足2页,则不显示分页导航
    if ($maxpage <= 1) {
        return '';
    }
    if ($page == 1) {
        $first = '<span>首页</span>';
        $prev = '<span>上一页</span>';
    } else {
        $first = "<a href=\"{$url}1\">首页</a>";
        $prev = '<a href="' . $url . ($page - 1) . '">上一页</a>';
    }
    if ($page == $maxpage) {
        $next = '<span>下一页</span>';
        $last = '<span>尾页</span>';
    } else {
        $next = '<a href="' . $url . ($page + 1) . '">下一页</a>';
        $last = "<a href=\"{$url}{$maxpage}\">尾页</a>";
    }
    // 组合最终样式
    return "<p>当前位于:$page/$maxpage</p>$first $prev $next $last";
}

/**
 * 获取SQL中的分页部分
 * @param int $page 当前页码值
 * @param int $size 每页显示的条数
 * @return string 拼接后的结果
 */
function page_sql($page, $size)
{
    return ($page - 1) * $size . ',' . $size;
}

init.php

<?php
// 在项目中设置时区,以适应各种服务器环境
date_default_timezone_set('Asia/Shanghai');
mb_internal_encoding('UTF-8');
// 连接数据库
$link = @mysqli_connect('127.0.0.1', 'root', 'root', 'php_wish');
if (!$link) {
    exit('数据库连接失败:' . mysqli_connect_error());
}
mysqli_set_charset($link, 'utf8');

网页样式有需要在留言吧

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-08-17 15:09:09  更:2021-08-17 15:09:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/15 4:23:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码