Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class NariChat
- {
- public function main()
- {
- if ($_POST['tgf'])
- {
- if ($_POST['msg'])
- {
- NariChat::putContents('msg');
- }
- NariChat::redirect('index.php');
- }
- }
- public static function putContents($str)
- {
- $conn = DB::connect();
- $msg = filter_input(INPUT_POST, $str);
- $dao = DaoChat();
- $dao->put($conn, $msg);
- $conn->commit();
- }
- public static function getContents()
- {
- $conn = DB::connect();
- $dao = DaoChat();
- $msgs = $dao->get($conn);
- return htmlspecialchars(var_export($msgs,true));
- }
- public static function redirect($url)
- {
- ob_start();ob_end_clean();
- header('Location:'.$url);
- exit;
- }
- }
- $cls = new NariChat();
- $cls->main();
- ?><!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="utf-8" />
- <title>ナリチャット</title>
- </head>
- <body>
- <h1>ナリチャット</h1>
- <p>JAPANESE ONRY m(__)m</p>
- <marquee>welcome</marquee>
- <p>工事中<p>
- <p><a href="#" onclick="javascript:alert('工事中')">過去ログ検索</a></p>
- <form action="index.php" method="POST">
- <input name="msg" />
- <input name="tgf" type="submit" />
- </form>
- <p>
- <pre><?php echo NariChat::getContents() ?></pre>
- </p>
- </body>
- </html>
- <?php
- class DB
- {
- const HOST = 'localhost';
- const DBNAME = 'ixi';
- const USER = 'ixi';
- const PASS = 'pass';
- public static function connect()
- {
- try {
- $pdo = new PDO(
- 'mysql:host='.self::HOST.';dbname='.self::DBNAME.';charset=utf8',
- self::USER,
- self::PASS,
- array(PDO::ATTR_EMULATE_PREPARES => false)
- );
- } catch (PDOException $e) {
- echo 'データベース接続失敗。'.$e->getMessage();
- exit;
- }
- return $pdo;
- }
- }
- define(DATETIME_MYSQL, 'Y-m-d H:i:s');
- class DaoChat
- {
- const TBL_NAME_CHAT = 'CHAT';
- public function put(PDO $conn, $msg)
- {
- $sql = '
- insert into
- '.self::TBL_NAME_CHAT.'
- (
- REG_DT
- ,UPD_DT
- ,MSG
- ) values (
- :REG_DT
- ,:UPD_DT
- ,:MSG
- )';
- $stmt = $conn->prepare($sql);
- $stmt->bindValue(':REG_DT', date(DATETIME_MYSQL));
- $stmt->bindValue(':UPD_DT', date(DATETIME_MYSQL));
- $stmt->bindValue(':MSG', $msg);
- return $stmt->execute;
- }
- public function get(PDO $conn)
- {
- $sql = '
- select
- *
- from
- CHAT
- order by
- UPD_DT desc
- ';
- $stmt = $conn->prepare($sql);
- $stmt->execute;
- return $stmt->fetchAll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement