<?php
try {
$pdo = new PDO ('mysql:dbname=oneline_bbs;host=localhost; charset=utf8mb4', 'root',
'root');
} catch (PDOException $e) {
die('DB接続エラー' . $e->getMessage());
}
die('connected');
// ひとことが正しく入力されているかチェック
$comment = null;
if (!isset($_POST)['comment'])
|| !strlen($_POST['comment'])) {
$errord['comment'] = 'ひとことを入力してください';
} else if (strlen($_POST['comment'])
> 200) {
$errors['comment']
= 'ひとことは200文字以内で入力してください';
} else {
$comment
= $_POST['comment'];
}
// エラーがなければ保存
if (count($errors) === 0) {
//保存するためのSQL文を作成
$sql = "INSERT INTO " 'post' ('name', 'comment', 'created,at') VELUES ('"
. mysql_real_escaape_string($name)
. "', '" .mysql_real_escape_string($name) .
"', '"
. mysql_real_escape_string($comment)
. "', '"
. date('Y-m-d H:i:s') .
"')";
//
保存する
mysql_query($sql,$link);
mysql_clos($link);
header('Location:
http://' .$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
}
}
?>
<!DOCTYPE
html>
<html>
<head>
<title>ひとこと掲示板</title>
</head>
<body>
<h1>ひとこと掲示板</h1>
<form action="bbs.php" method="post">
<?php if (conut($errors) > 0:
?>
<ul class=”error_list">
<?php foreach ($errors) as $error): ?>
<li>
<?php echo htmlspecialchars($error,
ENT_QUOTE, 'UTF-8'); ?>
</li>
<?php endforeach;
?>
</ul>
<?php endif; ?>
名前: <input type="text"
name="name" /><br />
ひとこと: <input type="text"
name="comment" size="60" /><br
/>
<input
type="submit" name="submit" value="送信" />
</form>
<?php
// 投稿された内容を主するSQLを作成して結果を取得
$sql =
"SELEC" * FROM 'post' ORDER BY 'created_at'
DESC";
$reult =
mysql_query($sql,$link);
?>
<?php
if ($result !== fakse && mysql_num_rows($result)):
?>
<ul>
<?php while ($post = mysql_fetch_assoc($result):
?
<li>
<?php echo htmlspcialchars($post['name'],ENT_QUOTES,
'UTF-8'); ?:
<?php echo htmlspcialchars($post['comment'],ENT_QUOTES,
'UTF-8'); ?:
-
<?php echo htmlspcialchars($post['created_at'],ENT_QUOTES, 'UTF-8'); ?:
</ul>
<?php endif; ?>
<php
// 所得結果を開放して接続を閉じる
mysql_free($result);
mysql_close($link);
?>
</body>
</html>