<?php
require "${_SERVER['DOCUMENT_ROOT']}/database.php";


if (!(empty ($_POST['thread-name'])))
  {
    $explain = '<p>' . $_POST['thread-explain'] . '</p>';
    $explain = str_replace ("\n", '</p><p>', $explain);

    $mysqli = set_mysql ('miteruzo_bbs');

    $mysqli -> set_charset ('utf8');

    $result = $mysqli -> query ('SELECT COUNT(*) FROM threads');
    $row = $result -> fetch_assoc ();
    $current = $row['COUNT(*)'];

    $result -> close ();

    $sql = "INSERT INTO threads (title, `explain`, latest, length) VALUES ('{$_POST['thread-name']}', '$explain', '" . date ('Y-m-d H:i:s') . "', 0)";
    $mysqli -> query ($sql);
/*  $sql = "CREATE TABLE `miteruzo_bbs`.`thread_$current` (
            `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'レス番',
            `name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '名なしさん' COMMENT '名前',
            `message` MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'レス',
            `date` DATETIME NOT NULL COMMENT '投稿日時',
            `image` VARCHAR( 31 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '画像 URL',
            `held` TINYINT( 1 ) NOT NULL DEFAULT '0' COMMENT '保留',
            `deleted` TINYINT( 1 ) NOT NULL DEFAULT '0' COMMENT '削除済',
            `pass` VARCHAR( 64 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '削除用パスワード',
            `good` INT( 11 ) NOT NULL DEFAULT '0' COMMENT '高評価数',
            `bad` INT( 11 ) NOT NULL DEFAULT '0' COMMENT '低評価数',
            INDEX ( `date` )
            ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci"; */
    $mysqli -> query ($sql);

    $mysqli -> close ();
  }

header ("location: ../?thread=$current");

