57回目授業内容 PHP+DBで本格的なWebシステムを作る〜脱線
[授業][Dreamweaver][PHP]
PHP+DBで本格的なWebシステムを作る
mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('mydb');
-
- 文字コードをセットする
mysql_query('SET NAMES UTF8');
- SQLを実行する
<?php mysql_connect('localhost', 'root', 'root')or die(mysql_error()); mysql_select_db('mydb') or die(mysql_error()); mysql_query('SET NAMES UTF8'); mysql_query('INSERT INTO my_items SET id=4, maker_id=1, item_name="もも", price=210, keyword="缶詰,ピンク,甘い", sales=0, created="2010-08-01", modified="2010-08-01"') or die(mysql_error()); echo 'データを挿入しました。'; ?>
- フォームからの情報を保存するフォーム
input.php
<p>登録する商品の情報を記入して下さい。</p> <form id="frmInput" name="frmInput" method="post" action="input_do.php"> <dl> <dt> <label for="maker_id">メーカーID</label> </dt> <dd> <input name="maker_id" type="text" id="maker_id" size="10" maxlength="10"> </dd> <dt> <label for="item_name">商品名</label> </dt> <dd> <input name="item_name" type="text" id="item_name" size="35" maxlength="255"> </dd> <dt> <label for="price">価格</label> </dt> <dd> <input name="price" type="text" id="price" size="10" maxlength="10"> 円</dd> <dt> <label for="keyword">キーワード</label> </dt> <dd> <input name="keyword" type="text" id="keyword" size="50" maxlength="255"> </dd> </dl> <input type="submit" value="登録する"> </form>
input_do.php
<?php require('dbconnect.php'); $sql = sprintf('INSERT INTO my_items SET maker_id=%d, item_name="%s", price=%d, keyword="%s"', mysql_real_escape_string($_POST['maker_id']), mysql_real_escape_string($_POST['item_name']), mysql_real_escape_string($_POST['price']), mysql_real_escape_string($_POST['keyword']) ); mysql_query($sql) or die(mysql_error()); ?> <p>商品を登録しました。</p>
- データの一覧画面を作る
index.php
<?php require('dbconnect.php'); $recordSet = mysql_query('SELECT m.name, i.* FROM makers m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC'); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>一覧画面</title> <link href="style.css" rel="stylesheet" media="screen, print"> </head> <table width="100%"> <tr> <th scope="col">ID</th> <th scope="col">メーカー</th> <th scope="col">商品名</th> <th scope="col">価格</th> </tr> <?php while ($table = mysql_fetch_assoc($recordSet)) { ?> <tr> <td><?php print(htmlspecialchars($table['id'])); ?></td> <td><?php print(htmlspecialchars($table['name'])); ?></td> <td><?php print(htmlspecialchars($table['item_name'])); ?></td> <td><?php print(htmlspecialchars($table['price'])); ?></td> </tr> <?php } ?> </table> <body> </body> </html>
- 接続プログラムを共通プログラムにする
dbconnect.php
<?php mysql_connect('localhost', 'root', 'root') or die(mysql_error()); mysql_select_db('mydb'); mysql_query('SET NAMES UTF8'); ?>
require('dbconnect.php');
- 別テーブルとリレーションする
$recordSet = mysql_query('SELECT m.name, i.* FROM makers m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC');
- 件数の多いレコードをページで分ける「ページング」
index.php
$page = $_REQUEST['page']; if ($page =='') { $page = 1; } $page = max($page, 1); //最終ページを取得する $sql = 'SELECT COUNT(*) AS cnt FROM my_items'; $recordSet = mysql_query($sql); $table = mysql_fetch_assoc($recordSet); $maxPage = ceil($table['cnt'] / 5); $page = min($page, $maxPage); $start = ($page - 1) * 5; $recordSet = mysql_query('SELECT m.name, i.* FROM makers m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC LIMIT ' . $start . ',5');
<ul class="paging"> <li><a href="index.php?page=<?php print($page - 1); ?>">前のページへ</a></li> <li><a href="index.php?page=<?php print($page + 1); ?>">次のページへ</a></li> </ul>