Home > Article > Backend Development > How to use PHP to implement the advertising delivery management function of CMS system
How to use PHP to implement the advertising delivery management function of the CMS system
With the rapid development of the Internet, advertising on the Internet has become more and more common. For a website with a lot of content, ad trafficking functionality is essential. This article will introduce how to use PHP to implement the advertising management function in a CMS (content management system) system.
First, we need to create a database table to store advertising-related data. The following is a simple database table structure example:
CREATE TABLE ads ( id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(100) NOT NULL, description TEXT, banner VARCHAR(255) NOT NULL, start_date DATE, end_date DATE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
The above table structure includes fields such as the title, description, banner image, start date, end date, and creation and update time of the advertisement.
Next, we need to create a page for managing ads. On this page we can add, edit and delete ads. Here is a simple sample code:
<?php // 连接数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 检查数据库连接是否成功 if ($conn->connect_error) { die("数据库连接失败:" . $conn->connect_error); } // 处理添加广告的逻辑 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = $_POST['title']; $description = $_POST['description']; $banner = $_POST['banner']; $start_date = $_POST['start_date']; $end_date = $_POST['end_date']; // 构建SQL插入语句 $sql = "INSERT INTO ads (title, description, banner, start_date, end_date) VALUES ('$title', '$description', '$banner', '$start_date', '$end_date')"; if ($conn->query($sql) === TRUE) { echo "广告添加成功"; } else { echo "广告添加失败:" . $conn->error; } } // 获取广告列表 $sql = "SELECT * FROM ads"; $result = $conn->query($sql); ?> <!DOCTYPE html> <html> <head> <title>广告投放管理</title> </head> <body> <h1>广告投放管理</h1> <form method="POST"> <label for="title">标题:</label> <input type="text" name="title" id="title" required><br> <label for="description">描述:</label> <textarea name="description" id="description" required></textarea><br> <label for="banner">横幅图片 URL:</label> <input type="text" name="banner" id="banner" required><br> <label for="start_date">开始日期:</label> <input type="date" name="start_date" id="start_date" required><br> <label for="end_date">结束日期:</label> <input type="date" name="end_date" id="end_date" required><br> <input type="submit" value="添加广告"> </form> <h2>广告列表</h2> <table> <tr> <th>ID</th> <th>标题</th> <th>描述</th> <th>横幅图片</th> <th>开始日期</th> <th>结束日期</th> </tr> <?php while ($row = $result->fetch_assoc()) { ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['title']; ?></td> <td><?php echo $row['description']; ?></td> <td><?php echo $row['banner']; ?></td> <td><?php echo $row['start_date']; ?></td> <td><?php echo $row['end_date']; ?></td> </tr> <?php } ?> </table> </body> </html>
The above code creates a simple form for adding new ads. At the same time, a list of existing advertisements is also displayed, and detailed information can be viewed in a table.
Through the above code example, we can implement a basic ad delivery management function. Of course, this is just a simple example, and actual implementation may require more details to be considered, such as ad review, ad space management, etc. However, through this example, you can learn how to use PHP to implement the advertising management function in a CMS system.
The above is the detailed content of How to use PHP to implement the advertising delivery management function of CMS system. For more information, please follow other related articles on the PHP Chinese website!