Webよりデータを入力してデータベースを構築

※色がついているところをクリックするとその説明に飛。
:My SQL
:PHP等



<!-- 部品1:書き込みフォーム -->
<form method="POST" action="book.php">
<table border="1">
<tr>
<td>書籍ID</td>
<td><input type="int" name="id"></td>
</tr>
<tr>
<td>タイトル</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>著者吊</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>出版社</td>
<td>
<input type="text" name="pub"></td>
</tr>
<tr>
<td>値段</td>
<td>
<input type="text" name="price"></td>
</tr>
<tr>
<td>発行年</td>
<td>
<input type="text" name="year"></td>
</tr>
<tr>
<td>評価
<td>
<input type="text" name="point"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="書き込む">
</td>
</tr>
</table>
</form>
<!-- 部品2:データベースへの接続 -->
<?php
// 接続設定(サーバ/データベース/ユーザ/パスワード)
$sv = "localhost";
$dbname = "BOOKDB";
$user = "root";
// データベースに接続する
$conn = mysql_connect($sv, $user) or die("接続エラー"); mysql_select_db($dbname) or die("接続エラー");
?>

<!-- 部品3:メッセージ書き込みスクリプト -->
<?php
// データベースの文字列に変換する関数
function cnv_dbstr($string) {
$string = htmlspecialchars($string);
// $string = mb_convert_encoding($string, "SJIS", "SJIS");
if (!get_magic_quotes_gpc()) {
$string = addslashes($string);
}
return $string;
}

// POSTで送信されたか判別する
if ($_SERVER["REQUEST_METHOD"] == "POST") {

// フォームからデータを受け取る
$id = cnv_dbstr($_POST["id"]);
$name = cnv_dbstr($_POST["name"]);
$author = cnv_dbstr($_POST["author"]);
$pub = cnv_dbstr($_POST["pub"]);
$year = cnv_dbstr($_POST["year"]);
$point = cnv_dbstr($_POST["point"]);
if (!empty($id) and !empty($bookname)){
// データを追加する
$sql = "INSERT INTO bookdata(id, name, author, pub, price, year, point)";
$sql .= "VALUES(";
$sql .= "'" . $id . "',";
$sql .= "'" . $name . "',";
$sql .= "'" . $author . "',";
$sql .= "'" . $pub . "',";
$sql .= "'" . $price . "',";
$sql .= "'" . $year . "',";
$sql .= "'" . $point . "',";
$sql .= ")";$res = mysql_query($sql, $conn) or die("データ追加エラー");
if ($res) {
echo "

1件書き込みました";
}
}
else {
echo "データを入力してください。";
}
}
?>

<!-- 部品4:メッセージ表示スクリプト -->
<?php
// 表示する文字列に変換する関数
function cnv_dispstr($string) {
// $string = mb_convert_encoding($string, "SJIS", "EUC-JP");
$string =nl2br($string);
return $string;
}

$sql = "SELECT * FROM booktable ORDER BY id DESC";
$res = mysql_query($sql, $conn) or die("データ抽出エラー");

while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
echo "<hr>";
echo cnv_dispstr($row["id"]);
echo cnv_dispstr($row["bookname"]);
echo cnv_dispstr($row["author"]);
echo cnv_dispstr($row["pub"]);
echo cnv_dispstr($row["price"]);
echo cnv_dispstr($row["year"]);
echo cnv_dispstr($row["point"]);
}
?>