许愿墙项目
项目图
效果图
连接数据库错误的可以看看我上一篇文章
所需文件





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');
网页样式有需要在留言吧
156

被折叠的 条评论
为什么被折叠?



