Home > Article > Backend Development > Analysis of event registration and ticket management functions of PHP social media application
Analysis of event registration and ticket management functions of PHP social media application
With the rise of social media, more and more activities are beginning to use social media for promotion and registration. In order to better manage event registration and ticketing, it is particularly important to develop a PHP application with corresponding functions. This article will introduce how to implement the event registration and ticket management functions of social media applications through PHP, and provide relevant code examples.
CREATE TABLE events ( id INT(11) PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, description TEXT, start_date DATETIME NOT NULL, end_date DATETIME NOT NULL, capacity INT(11) NOT NULL ); CREATE TABLE participants ( id INT(11) PRIMARY KEY AUTO_INCREMENT, event_id INT(11), name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, ticket_code VARCHAR(255) );
<?php // 连接数据库 $db = new PDO('mysql:host=localhost;dbname=your_database_name;charset=utf8mb4', 'your_username', 'your_password'); // 查询所有活动 $query = $db->query('SELECT * FROM events'); $events = $query->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>活动列表</title> </head> <body> <h1>活动列表</h1> <?php foreach ($events as $event): ?> <h2><?php echo $event['title']; ?></h2> <p><?php echo $event['description']; ?></p> <p>开始时间:<?php echo $event['start_date']; ?></p> <p>结束时间:<?php echo $event['end_date']; ?></p> <p>报名人数:<?php echo getParticipantCount($event['id']); ?></p> <?php if (hasAvailableTickets($event['id'])): ?> <a href="signup.php?event_id=<?php echo $event['id']; ?>">报名</a> <?php else: ?> <p>名额已满</p> <?php endif; ?> <?php endforeach; ?> </body> </html>
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 处理报名表单提交 $name = $_POST['name']; $event_id = $_GET['event_id']; // 生成票码 $ticket_code = generateTicketCode(); // 将报名信息保存到数据库 $db->prepare('INSERT INTO participants (event_id, name, email, ticket_code) VALUES (?, ?, ?, ?)') ->execute([$event_id, $name, $email, $ticket_code]); // 跳转到报名成功页面 header('Location: success.php'); exit; } else { $event_id = $_GET['event_id']; $query = $db->prepare('SELECT * FROM events WHERE id = ?'); $query->execute([$event_id]); $event = $query->fetch(PDO::FETCH_ASSOC); } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>报名</title> </head> <body> <h1>报名</h1> <h2><?php echo $event['title']; ?></h2> <form method="POST" action=""> <label>姓名:</label> <input type="text" name="name" required> <br> <label>邮箱:</label> <input type="email" name="email" required> <br> <button type="submit">确认报名</button> </form> </body> </html>
<?php $query = $db->prepare('SELECT * FROM participants WHERE ticket_code = ?'); $query->execute([$ticket_code]); $participant = $query->fetch(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>报名成功</title> </head> <body> <h1>报名成功</h1> <p>您已成功报名 <?php echo $event['title']; ?> 活动!</p> <p>您的票码是:<?php echo $participant['ticket_code']; ?></p> </body> </html>
Through the above code example, we can implement the event registration and ticket management functions of a simple PHP social media application. Of course, we can further improve and expand these functions based on actual needs. Hope this article helps you!
The above is the detailed content of Analysis of event registration and ticket management functions of PHP social media application. For more information, please follow other related articles on the PHP Chinese website!