<?php
require_once 'db.php';
// フォームから送信されたかチェック
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
header("Location: index.php");
exit;
}
// 送信されたデータを取得
$username = $_POST["username"] ?? "";
$content = $_POST["content"] ?? "";
// 空チェック
if (empty($username) || empty($content)) {
header("Location: index.php");
exit;
}
$pdo = getDb();
// データを挿入
$stmt = $pdo->prepare("INSERT INTO tweets (username, content) VALUES (?, ?)");
$stmt->execute([$username, $content]);
// index.phpにリダイレクト
header("Location: index.php");
exit;
?>