2012年3月5日月曜日

開発環境

  • OS X Lion - Apple(OS)
  • Apache (Web Server)
  • PHP (サーバーサイドプログラミング言語)
  • MySQL (データベース)
  • TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)

『初めてのPHP & MySQL 第2版』(Michele E. DavisJon A. Phillips 著、西沢 直木 訳、オライリー・ジャパン、2008年、ISBN978-4-87311-365-4)の10章(フォームと連携)の問題を解いてみる。

問10-1.

フォーム入力値を処理する際に、同じスクリプトを呼び出すのに使用できるスーパーグローバル変数は、$_SERVER['PHP_SELF']。

問10-2, 3.

コード(TextWrangler)

<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
    <title></title>
  </head>
  <body>
  <?php
    $username = htmlentities($_GET['username']);
    $passwd = htmlentities($_GET['passwd']);
    $self = htmlentities($_SERVER['PHP_SELF']);
    if($username == ""){
      echo ("
        <form action=\"$self\" method=\"GET\">
          <label>ユーザ名: <input type=\"text\" name=\"username\"/></label><br />
          <label>パスワード: <input type=\"password\" name=\"passwd\"/></label><br />
          <input type=\"submit\" value=\"送信\" />
        </form>");
    } else {
      echo "ユーザ名: $username<br />" .
           "パスワード: $passwd<br />";
    }
  ?>
  </body>
</html>

出力結果(ブラウザ)

ユーザ名: 
パスワード: 
// あと各種ボタンとか

HTMLソース

<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
    <title></title>
  </head>
  <body>
  
        <form action="/~kamimura/kamimura_blog/learning_php_and_mysql/sample60.php" method="GET">
          <label>ユーザ名: <input type="text" name="username"/></label><br />
          <label>パスワード: <input type="password" name="passwd"/></label><br />
          <input type="submit" value="送信" />
        </form>  </body>
</html>

ユーザ名を入力せずに送信するとまた上記の出力結果、HTMLソースになる。ユーザ名(以下の結果はkamimura、パスワードはpassword)を入力して送信したとき。

出力結果(ブラウザ)

ユーザ名: kamimura
パスワード: password

HTMLソース

<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
    <title></title>
  </head>
  <body>
  ユーザ名: kamimura<br />パスワード: password<br />  </body>
</html>

問10-4.

「A.」から始まる著者を抽出するクエリ。

<?php
  $query = "select author_id, title_id, author from authors where author like 'A.%'";
?>

併せて読んでいる書籍。

Pythonの学習が1周したら上記の2冊を順に取り組む計画。それまではひたすら復習!

0 コメント:

コメントを投稿