如果您刚刚开始使用 PHP,您可以承担的最令人兴奋的项目之一就是构建数据库驱动的 Web 应用程序。这是了解后端如何工作、与数据库交互并向用户提供动态内容的好方法。
在本教程中,我们将使用 PHP 和 MySQL 构建一个简单的 待办事项列表应用程序。最后,您将拥有一个可以运行的应用程序,用户可以在其中添加、查看和删除任务。
先决条件
在我们深入之前,请确保您已经:
- PHP(7.4 或更高版本)
- MySQL(或 MariaDB)
- 像 XAMPP 或 Laragon 这样的本地服务器
- 类似 VS Code 的代码编辑器
第 1 步:设置您的环境
- 安装本地服务器(例如 XAMPP)。
- 启动 Apache 和 MySQL 服务。
- 导航到您的 Web 根目录(XAMPP 的 htdocs)并创建一个名为 todo_app 的新文件夹。
第 2 步:创建数据库
- 打开 phpMyAdmin。
- 创建一个名为 todo_app 的新数据库。
- 运行以下 SQL 查询来创建任务表:
sql CREATE TABLE tasks ( id INT AUTO_INCREMENT PRIMARY KEY, task VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
第 3 步:构建 HTML 前端
在 todo_app 文件夹中创建 index.php 文件并添加以下 HTML:
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>To-Do List App</title> <link rel="stylesheet" href="styles.css"> <div> <hr> <h2> Step 4: Handle Adding Tasks </h2> <p>Create a new file called <em>add_task.php</em> and add the following code:<br> </p> <pre class="brush:php;toolbar:false"><?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $task = $_POST['task']; // Connect to the database $conn = new mysqli("localhost", "root", "", "todo_app"); // Insert the task into the database $stmt = $conn->prepare("INSERT INTO tasks (task) VALUES (?)"); $stmt->bind_param("s", $task); $stmt->execute(); $stmt->close(); $conn->close(); // Redirect back to the main page header("Location: index.php"); exit(); } ?>
第 5 步:处理删除任务
创建一个名为delete_task.php:
的新文件
<?php if (isset($_GET['id'])) { $id = $_GET['id']; // Connect to the database $conn = new mysqli("localhost", "root", "", "todo_app"); // Delete the task from the database $stmt = $conn->prepare("DELETE FROM tasks WHERE id = ?"); $stmt->bind_param("i", $id); $stmt->execute(); $stmt->close(); $conn->close(); // Redirect back to the main page header("Location: index.php"); exit(); } ?>
第 6 步:添加一些 CSS(可选)
在同一文件夹中创建 styles.css 文件来设置应用程序的样式:
body { font-family: Arial, sans-serif; background-color: #f9f9f9; color: #333; margin: 0; padding: 0; } .container { width: 50%; margin: 50px auto; background: #fff; padding: 20px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); border-radius: 8px; } h1 { text-align: center; } form { display: flex; justify-content: space-between; margin-bottom: 20px; } form input { flex: 1; padding: 10px; margin-right: 10px; border: 1px solid #ccc; border-radius: 4px; } form button { padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 4px; cursor: pointer; } form button:hover { background-color: #218838; } ul { list-style-type: none; padding: 0; } ul li { display: flex; justify-content: space-between; padding: 10px; border-bottom: 1px solid #ddd; } ul li a { color: #dc3545; text-decoration: none; }
第 7 步:运行您的应用程序
- 打开浏览器并转到http://localhost/todo_app/index.php。
- 添加一些任务、查看它们并删除它们。 ?
恭喜!您刚刚使用 PHP 和 MySQL 构建了第一个数据库驱动的 Web 应用程序。这个简单的项目为创建更复杂的应用程序奠定了基础。尝试添加任务优先级或用户身份验证等功能。
如果您喜欢本教程,请发表评论或与其他开发人员分享。快乐编码! ?
以上是PHP 初学者:构建您的第一个数据库驱动的 Web 应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

PHP会话对应用性能有显着影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器