<?php
// Display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
var_dump($_POST['contentData']); // Check the contentData
var_dump($_FILES); // Check the uploaded files
// Database connection
$host = 'sql305.epizy.com'; //infinityfree
$db = 'epiz_37717046_website_data'; //inf0
$user = 'epiz_37717046'; //inf0
$pass = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
$title = $_POST['title'] ?? null;
$author = $_POST['author'] ?? null;
$created_at = $_POST['created-at'] ?? null; // Form- 'created-at'
$tags = $_POST['tags'] ?? null; // Tags field
if (!$title || !$author || !$created_at) {
die("Missing required fields.");
}
try {
$stmt = $pdo->prepare(
"INSERT INTO `blog_posts` (`title`, `author`, `created_at`) VALUES (?, ?, ?)"
);
$stmt->execute([$title, $author, $created_at]);
$post_id = $pdo->lastInsertId();
} catch (PDOException $e) {
die("Error inserting post: " . $e->getMessage());
}
// Handle tags
if ($tags) {
$tags = explode(',', $tags); // tags are comma-separated
foreach ($tags as $tag) {
$tag = trim($tag);
// Check if the tag exists
$stmt = $pdo->prepare("SELECT id FROM tags WHERE name = ?");
$stmt->execute([$tag]);
$tagId = $stmt->fetchColumn();
if (!$tagId) {
$stmt = $pdo->prepare("INSERT INTO tags (name, slug) VALUES (?, ?)");
$slug = strtolower(str_replace(' ', '-', $tag)); // Create a slug from the tag name
$stmt->execute([$tag, $slug]);
$tagId = $pdo->lastInsertId(); // Get the new tag's ID
}
$stmt = $pdo->prepare("INSERT INTO post_tags (post_id, tag_id) VALUES (?, ?)");
$stmt->execute([$post_id, $tagId]);
}
}
//upload image
$image_url = null; // Default image URL to null
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$imageTmpPath = $_FILES['image']['tmp_name'];
$imageName = uniqid() . '-' . basename($_FILES['image']['name']); // unique names
$uploadDir = 'uploads/images/';
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
$uploadPath = $uploadDir . $imageName;
if (move_uploaded_file($imageTmpPath, $uploadPath)) {
$image_url = $uploadPath;
} else {
echo json_encode([
'success' => false,
'message' => 'Failed to move uploaded file'
]);
exit;
}
}
if (isset($_POST['contentData'])) {
$contentData = json_decode($_POST['contentData'], true);
// Prepare to store each piece of content
foreach ($contentData as $content) {
$type = $content['type'];
$order = $content['order'];
$text_content = $content['content'] ?? null;
$alt = $content['alt'] ?? null;
$figcaption = $content['figcaption'] ?? null;
try {
if ($type === 'text') {
$stmt = $pdo->prepare(
"INSERT INTO `post_content` (`post_id`, `content_type`, `content`, `order`)
VALUES (?, 'text', ?, ?)"
);
$stmt->execute([$post_id, $text_content, $order]);
} elseif ($type === 'image' && $image_url !== null) {
$stmt = $pdo->prepare(
"INSERT INTO `post_content` (`post_id`, `content_type`, `image_url`, `alt`, `figcaption`, `order`)
VALUES (?, 'image', ?, ?, ?, ?)"
);
$stmt->execute([$post_id, $image_url, $alt, $figcaption, $order]);
}
} catch (PDOException $e) {
die(json_encode([
'success' => false,
'message' => 'Error inserting content: ' . $e->getMessage()
]));
}
}
}
// Success response
echo json_encode(['success' => true, 'message' => 'Post saved successfully']);
} else {
die("Invalid request method.");
}
?>