如果您剛開始使用 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中文網其他相關文章!

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

設置httponly標誌對會話cookie至關重要,因為它能有效防止XSS攻擊,保護用戶會話信息。具體來說,1)httponly標誌阻止JavaScript訪問cookie,2)在PHP和Flask中可以通過setcookie和make_response設置該標誌,3)儘管不能防範所有攻擊,但應作為整體安全策略的一部分。

phpsessions solvathepromblymaintainingStateAcrossMultipleHttpRequestsbyStoringDataTaNthEserVerAndAssociatingItwithaIniquesestionId.1)他們儲存了AtoredAtaserver side,通常是Infilesordatabases,InseasessessionIdStoreDistordStoredStoredStoredStoredStoredStoredStoreDoreToreTeReTrestaa.2)

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.更新用戶端會話信息。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器