PHP 栏目功能通常用于网站后台管理系统中,它可以帮助管理员方便地管理网站的分类信息,包括添加、编辑、删除栏目等操作。下面是一个简单的 PHP 栏目功能的代码示例,它使用了 PHP 和 MySQL:
<?php // 连接数据库 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 处理添加栏目操作 if (isset($_POST['add'])) { $name = $_POST['name']; $parent_id = $_POST['parent_id']; $sql = "INSERT INTO categories (name, parent_id) VALUES ('$name', $parent_id)"; if ($conn->query($sql) === TRUE) { echo "添加成功"; } else { echo "添加失败: " . $conn->error; } } // 处理编辑栏目操作 if (isset($_POST['edit'])) { $id = $_POST['id']; $name = $_POST['name']; $parent_id = $_POST['parent_id']; $sql = "UPDATE categories SET name='$name', parent_id=$parent_id WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "更新成功"; } else { echo "更新失败: " . $conn->error; } } // 处理删除栏目操作 if (isset($_GET['delete'])) { $id = $_GET['delete']; $sql = "DELETE FROM categories WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "删除成功"; } else { echo "删除失败: " . $conn->error; } } // 查询栏目列表数据 $sql = "SELECT * FROM categories"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table><tr><th>ID</th><th>名称</th><th>父级栏目</th><th>操作</th></tr>"; while($row = $result->fetch_assoc()) { echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["parent_id"]."</td><td><a href='edit.php?id=".$row["id"]."'>编辑</a> <a href='?delete=".$row["id"]."'>删除</a></td></tr>"; } echo "</table>"; } else { echo "0 结果"; } $conn->close(); ?>
这段代码实现了一个简单的栏目功能,具体实现过程如下:
评论