Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
<?php // Insert a post $pdo->prepare("INSERT INTO posts (title, content, user_id) VALUES (?, ?, ?)") ->execute([$title, $content, $userId]); $postId = (int) $pdo->lastInsertId(); // Now insert related tags using the new post ID $stmt = $pdo->prepare("INSERT INTO post_tags (post_id, tag_id) VALUES (?, ?)"); foreach ($tagIds as $tagId) { $stmt->execute([$postId, $tagId]); } echo "Post {$postId} created with " . count($tagIds) . " tags";
Result
Open