<?php
include("../incl/functions.php");
if (isset($_POST["parent"]) && isset($_POST["name"]) && isset($_POST["text"]) && isset($_POST["referer"])) {
    $stmt = create_statement("INSERT INTO comments VALUES(?, ?, ?, ?, ?)");
    $params = [rand(0, PHP_INT_MAX), date('Y-m-d H:i:s'), $_POST["parent"], $_POST["name"], $_POST["text"]];
    try {
        $stmt->execute($params);
        header("Location: {$_POST["referer"]}");
    } catch (PDOException $e) {
        echo "<p>Failed to insert a new row, dumping details for debug.</p>";
        echo "<p>Incoming \$_POST:<pre>" . print_r($_POST, true) . "</pre>";
        echo "<p>The error code: " . $stmt->errorCode();
        echo "<p>The error message:<pre>" . print_r($stmt->errorInfo(), true) . "</pre>";
        throw $e;
    }
} else {
    echo "ERROR: Did not get all needed data from the post.";
}
