
PHP
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>簡易掲示板</title>
</head>
<body>
<h2>入力フォーム</h2>
<form action="" method="POST">
<input type="hidden" name="editnumber" size='20' value='' placeholder="名前"><br />
<input type="text" name="name" size='20' value='' placeholder="名前"><br />
<input type="text" name="comment" size='20' value='' placeholder="コメント"><br />
<input type="password" name="password" size='20' value='' placeholder="パスワード"><br />
<br />
<input type="submit" name='submit' value='送信' />
</form>
<form action="" method="POST">
<h2>削除フォーム</h2>
<input type="number" name="deletenumber" size='20' placeholder="削除番号"><br />
<input type="password" name="deletepassword" placeholder="パスワード"><br />
<br />
<input type="submit" name='delete' value='削除' onclick="return confirm('削除しますか?')" />
<br />
<br />
</form>
<form action="" method="POST">
<h2>編集フォーム</h2>
<input type="text" name="editnumber" placeholder="編集番号"><br />
<input type="password" name="editpassword" placeholder="パスワード"><br />
<br />
<input type="submit" name="edit" value="編集"><br />
<br />
<br />
</form>
<?php
// データベースへ接続
try {
$dsn = 'mysql:dbname=co_19_349_99sv_coco_com;host=localhost';
$user = 'co-19-349.99sv-c';
$password = 'U2j9s5dQ';
$pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
} catch (PDOException $e) {
echo 'DB接続エラー' . $e->getMessage;
}
// リセット用
// $sql = "DROP TABLE kadai2a";
// $pdo->query($sql);
// テーブルを作成
$sql = "CREATE TABLE IF NOT EXISTS kadai2a"
. "("
. "id INT AUTO_INCREMENT PRIMARY KEY,"
. "name char(32),"
. "comment TEXT,"
. "date DATETIME,"
. "password TEXT"
. ");";
$stmt = $pdo->query($sql);
// 新規投稿
if (isset($_POST['submit'])) {
if (isset($_POST['name']) && (isset($_POST['comment'])) && (isset($_POST['password']))) {
$sql = $pdo->prepare("INSERT INTO kadai2a (name,comment,date,password) VALUES (:name,:comment,:date,:password)");
$sql->bindParam(':name', $name, PDO::PARAM_STR);
$sql->bindParam(':comment', $comment, PDO::PARAM_STR);
$sql->bindParam(':date', $date, PDO::PARAM_STR);
$sql->bindParam(':password', $password, PDO::PARAM_STR);
$name = $_POST['name'];
$comment = $_POST['comment'];
date_default_timezone_set('Asia/Tokyo');
$date = date("Y/m/d H:i:s");
$password = $_POST['password'];
$sql->execute();
}
if ((empty($name)) || (empty($comment)) || (empty($password))) {
echo "<script>alert('未入力項目があります')</script>";
}
}
// 編集
if (isset($_POST['edit'])) {
if ((isset($_POST['editnumber'])) && (isset($_POST['editpassword']))) {
$editnumber = $_POST['editnumber'];
$editpassword = $_POST['editpassword'];
$results = $pdo->query("SELECT * FROM kadai2a WHERE id= $editnumber");
foreach ($results as $result) {
if ($editnumber == $result['id'] && $editpassword == $result['password']) {
$editnumber = $result['id'];
$editname = $result['name'];
$editcomment = $result['comment'];
$editpassword = $result['password'];
echo "編集番号" . $editnumber
?>
<form action="<?php echo ($_SERVER['PHP_SELF']) ?>" method="POST">
<input type="hidden" name="editnumber" value="<?php if (isset($editnumber)) {
echo $editnumber;
} ?>">
<h3>名前:</h3>
<input type="text" name="editname" value="<?php if (isset($editname)) {
echo $editname;
} ?>">
<h3>コメント:</h3>
<input type="text" name="editcomment" value="<?php if (isset($editcomment)) {
echo $editcomment;
} ?>">
<h3>パスワード:</h3>
<input type="password" name="editpassword" value="<?php if (isset($editpassword)) {
echo $editpassword;
} ?>"><br />
<br />
<input type="submit" name="editsubmit" value="編集"><br /><br /><br />
</form>
<?php
break;
} elseif ($editnumber == $result['id'] && $editpassword != $result['password']) {
echo "<script>alert('パスワードが違います')</script>";
}
}
}
}
if (isset($_POST['editsubmit'])) {
if (!empty($_POST["editname"]) && (!empty($_POST["editcomment"])) && (!empty($_POST["editpassword"]))) {
try {
$dsn = 'mysql:dbname=co_19_349_99sv_coco_com;host=localhost';
$user = 'co-19-349.99sv-c';
$password = 'U2j9s5dQ';
$pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$editnumber = $_POST['editnumber'];
$name = $_POST['editname'];
$comment = $_POST['editcomment'];
date_default_timezone_set('Asia/Tokyo');
$date = date("Y/m/d H:i:s");
$password = $_POST['editpassword'];
$sql = "UPDATE kadai2a SET name=:name,comment=:comment,date=:date,password=:password WHERE id=$editnumber";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':name' => $name, ':comment' => $comment, ':date' => $date, ':password' => $password));
} catch (PDOException $e) {
echo 'DB接続エラー' . $e->getMessage;
}
}
}
// 削除
if (isset($_POST['delete'])) {
if ((isset($_POST['delete'])) && (isset($_POST['deletepassword']))) {
$deletenumber = $_POST['deletenumber'];
$deletepassword = $_POST['deletepassword'];
$res = $pdo->query('SELECT * FROM kadai2a');
foreach ($res as $value) {
if ($deletenumber == $value['id'] && $deletepassword == $value['password']) {
$id = $deletenumber;
$sql = 'delete from kadai2a where id=:id';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
} elseif ($deletenumber == $value['id'] && $deletepassword != $value['password']) {
echo "<script>alert('パスワードが違います')</script>";
}
}
}
}
// データを表示する
$sql = 'SELECT * FROM kadai2a';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
foreach ($results as $row) {
echo $row['id'] . ',';
echo $row['name'] . ',';
echo $row['comment'] . ',';
echo $row['date'] . '<br>';
echo "<hr>";
}
?>
</body>
</html>