Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Board1作成しました。 #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions board1/DbManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
function getDb()
{
$dsn = 'mysql:dbname=board1_ab; host=127.0.0.1';
$user = 'root';
$passwd = '';

try {
//データベースへの接続を確立
$db = new PDO($dsn, $user, $passwd);
//データベース接続時に使用する文字コードをutf8に設定
$db->exec('SET NAMES utf8');

print '接続に成功しました。';
} catch (PDOException $e) {
die("接続エラー:{$e->getMessage()}");
}
return $db;
}
?>


4 changes: 4 additions & 0 deletions board1/Encode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function e($str, $charset = 'UTF-8') {
print htmlspecialchars($str, ENT_QUOTES, $charset);
}
56 changes: 56 additions & 0 deletions board1/insert_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<html>
<head>
<title>データの登録</title>
</head>

<body>
<form method="POST" action="insert_process.php">
<p>
ID:<br />
<input type="text" name="id" size="25" maxlength="20" />
</p><p>
名前:<br />
<input type="text" name="name" size="25" maxlength="150" />
</p><p>
内容:<br />
<input type="text" name="contents" size="25" maxlength="500" />
</p><p>
<input type="submit" value="投稿">
</p>
</form>

<head>
<title>結果セット</title>
</head>
<body>
<table border="1">
<tr>
<th>ID</th><th>名前</th><th>内容</th>
</tr>
<?php
require_once './DbManager.php';
require_once './Encode.php';
try {
//データベースへの接続を確立
$db = getDb();
//select命令の実行
$stt = $db->prepare('SELECT * FROM post_table ORDER BY contents DESC');
$stt->execute();
//結果セットの内容を順に出力
while($row = $stt->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><?php e($row['name']); ?></td>
<td><?php e($row['contents']); ?></td>
</tr>
<?php
}
$db = NULL;
} catch(PDOExeption $e) {
die("エラーメッセージ:{$e->getMessage()}");
}
?>
</table>

</body>
</html>
23 changes: 23 additions & 0 deletions board1/insert_process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
require_once './DbManager.php';

try {
//データベースへの接続を確立
$db = getDb();
//INSERT命令の準備
$stt = $db->prepare('INSERT INTO post_table(id, name, contents) VALUES(:id, :name, :contents)');
//INSERT命令にポストデータの内容をセット
$stt->bindValue(':name', $_POST['name']);
$stt->bindValue(':contents', $_POST['contents']);
//INSERT命令を実行
$stt->execute();
$db = NULL;
}catch(PDOException $e) {
die("エラーメッセージ:{$e->getMessage()}");
}

//処理後は入力フォームにリダイレクト
header('Location: http://'.$_SERVER['HTTP_HOST']
.dirname($_SERVER['PHP_SELF']).'/insert_form.php');


38 changes: 38 additions & 0 deletions board1/result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
require_once './DbManager.php'; //getDb関数の有効化
require_once './Encode.php'; //e関数の有効化
?>
<html>
<head>
<title>結果セット</title>
</head>
<body>
<table border="1">
<tr>
<th>ID</th><th>名前</th><th>内容</th>
</tr>
<?php
try {
//データベースへの接続を確立
$db = getDb();
//select命令の実行
$stt = $db->prepare('SELECT * FROM post_table ORDER BY contents DESC');
$stt->execute();
//結果セットの内容を順に出力
while($row = $stt->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><?php e($row['id']); ?></td>
<td><?php e($row['name']); ?></td>
<td><?php e($row['contents']); ?></td>
</tr>
<?php
}
$db = NULL;
} catch(PDOExeption $e) {
die("エラーメッセージ:{$e->getMessage()}");
}
?>
</table>
</body>
</html>
12 changes: 12 additions & 0 deletions hello_world/hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<title>PHPの基本</title>
</head>
<body>
<?php
// print 命令は指定された文字列を表示するための命令です。
print 'こんにちは、世界!<br />';
print 'こんにちは、皆さん!';
?>
</body>
</html>