许愿墙项目
项目图 效果图 连接数据库错误的可以看看我上一篇文章 所需文件 
 
  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);
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');
$name = mb_strimwidth($name, 0, 12);
$name = $name ?: '匿名';
if (!in_array($color, ['blue', 'yellow', 'green', 'red'])) {
$color = 'green';
}
$content = mb_strimwidth($content, 0, 80);
$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
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 . '”');
}
}
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秒';
}
function page_html($url, $total, $page, $size)
{
$maxpage = max(ceil($total / $size), 1);
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";
}
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');
网页样式有需要在留言吧
|