※色がついているところをクリックするとその説明に飛。 ■:My SQL ■:PHP等 | ||
<!-- 部品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"]);
}
?>