PHP+MySQL实现后台管理系统增删改查之够用就好

2024-03-05 02:52

本文主要是介绍PHP+MySQL实现后台管理系统增删改查之够用就好,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明

最近要给博客弄个后台,不想搞得很复杂,有基本的增删改查就够了,到网上找了一圈发现这个不错,很实用,希望可以帮到大家,需要的朋友评论区留下邮箱,我安排发送。

在这里插入图片描述

演示效果

在这里插入图片描述

项目介绍

本项目基于Php开发,实现最基本的增删改查页面,通常作为简单的后台管理系统使用。

技术栈

  • php7+
  • mysql5.7+

运行步骤

  1. 本机搭建好mysql数据库和php运行环境(推荐下载安装phpstudy软件,运行mysql和apache,PHP建议选择7.4+)
  2. 将本项目文件夹复制到web运行目录,例如:d:/phpstudy/www,phpstudy中创建网站并指向该目录
  3. mysql中创建数据库: phpcrud,执行脚本:phpcrud.sql
  4. 修改db.php文件中数据库密码
  5. 浏览器访问:localhost:端口/phpcrud

核心代码

首页

<?php
include 'functions.php';
// Your PHP code here.// Home Page template below.
?><?= template_header('Home') ?><div class="content"><h2>首页</h2><p>欢迎来到首页!</p>
</div><?= template_footer() ?>

列表查询

<?php
include 'functions.php';
// Connect to MySQL database
$pdo = pdo_connect_mysql();
// Get the page via GET request (URL param: page), if non exists default the page to 1
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
// Number of records to show on each page
$records_per_page = 5;
// Prepare the SQL statement and get records from our contacts table, LIMIT will determine the page
$stmt = $pdo->prepare('SELECT * FROM contacts ORDER BY id LIMIT :current_page, :record_per_page');
$stmt->bindValue(':current_page', ($page - 1) * $records_per_page, PDO::PARAM_INT);
$stmt->bindValue(':record_per_page', $records_per_page, PDO::PARAM_INT);
$stmt->execute();
// Fetch the records so we can display them in our template.
$contacts = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get the total number of contacts, this is so we can determine whether there should be a next and previous button
$num_contacts = $pdo->query('SELECT COUNT(*) FROM contacts')->fetchColumn();
?><?= template_header('Read') ?><div class="content read"><h2>联系人列表</h2><a href="create.php" class="create-contact">新增</a><table><thead><tr><td>#</td><td>姓名</td><td>邮箱</td><td>电话</td><td>级别</td><td>创建时间</td><td></td></tr></thead><tbody><?php foreach ($contacts as $contact) : ?><tr><td><?= $contact['id'] ?></td><td><?= $contact['name'] ?></td><td><?= $contact['email'] ?></td><td><?= $contact['phone'] ?></td><td><?= $contact['title'] ?></td><td><?= $contact['created'] ?></td><td class="actions"><a href="update.php?id=<?= $contact['id'] ?>" class="edit"><i class="fas fa-pen fa-xs"></i></a><a href="delete.php?id=<?= $contact['id'] ?>" class="trash"><i class="fas fa-trash fa-xs"></i></a></td></tr><?php endforeach; ?></tbody></table><div class="pagination"><?php if ($page > 1) : ?><a href="read.php?page=<?= $page - 1 ?>"><i class="fas fa-angle-double-left fa-sm"></i></a><?php endif; ?><?php if ($page * $records_per_page < $num_contacts) : ?><a href="read.php?page=<?= $page + 1 ?>"><i class="fas fa-angle-double-right fa-sm"></i></a><?php endif; ?></div>
</div><?= template_footer() ?>

新增页面

在这里插入图片描述

<?php
include 'functions.php';
$pdo = pdo_connect_mysql();
$msg = '';
// Check if POST data is not empty
if (!empty($_POST)) {// Post data not empty insert a new record// Set-up the variables that are going to be inserted, we must check if the POST variables exist if not we can default them to blank$id = isset($_POST['id']) && !empty($_POST['id']) && $_POST['id'] != 'auto' ? $_POST['id'] : NULL;// Check if POST variable "name" exists, if not default the value to blank, basically the same for all variables$name = isset($_POST['name']) ? $_POST['name'] : '';$email = isset($_POST['email']) ? $_POST['email'] : '';$phone = isset($_POST['phone']) ? $_POST['phone'] : '';$title = isset($_POST['title']) ? $_POST['title'] : '';$created = isset($_POST['created']) ? $_POST['created'] : date('Y-m-d H:i:s');// Insert new record into the contacts table$stmt = $pdo->prepare('INSERT INTO contacts VALUES (?, ?, ?, ?, ?, ?)');$stmt->execute([$id, $name, $email, $phone, $title, $created]);// Output message$msg = '新增成功!';
}
?><?= template_header('Create') ?><div class="content update"><h2>Create Contact</h2><form action="create.php" method="post"><label for="id">ID</label><label for="name">姓名</label><input type="text" name="id" placeholder="26" value="auto" id="id"><input type="text" name="name" placeholder="John Doe" id="name"><label for="email">邮箱</label><label for="phone">电话</label><input type="text" name="email" placeholder="johndoe@example.com" id="email"><input type="text" name="phone" placeholder="2025550143" id="phone"><label for="title">级别</label><label for="created">创建时间</label><input type="text" name="title" placeholder="Employee" id="title"><input type="datetime-local" name="created" value="<?= date('Y-m-d\TH:i') ?>" id="created"><input type="submit" value="Create"></form><?php if ($msg) : ?><p><?= $msg ?></p><?php endif; ?>
</div><?= template_footer() ?>

这篇关于PHP+MySQL实现后台管理系统增删改查之够用就好的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/775104

相关文章

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

SpringBoot使用ffmpeg实现视频压缩

《SpringBoot使用ffmpeg实现视频压缩》FFmpeg是一个开源的跨平台多媒体处理工具集,用于录制,转换,编辑和流式传输音频和视频,本文将使用ffmpeg实现视频压缩功能,有需要的可以参考... 目录核心功能1.格式转换2.编解码3.音视频处理4.流媒体支持5.滤镜(Filter)安装配置linu

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno

在Spring Boot中实现HTTPS加密通信及常见问题排查

《在SpringBoot中实现HTTPS加密通信及常见问题排查》HTTPS是HTTP的安全版本,通过SSL/TLS协议为通讯提供加密、身份验证和数据完整性保护,下面通过本文给大家介绍在SpringB... 目录一、HTTPS核心原理1.加密流程概述2.加密技术组合二、证书体系详解1、证书类型对比2. 证书获

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

MySQL 安装配置超完整教程

《MySQL安装配置超完整教程》MySQL是一款广泛使用的开源关系型数据库管理系统(RDBMS),由瑞典MySQLAB公司开发,目前属于Oracle公司旗下产品,:本文主要介绍MySQL安装配置... 目录一、mysql 简介二、下载 MySQL三、安装 MySQL四、配置环境变量五、配置 MySQL5.1

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可