SSブログ

「PHP」でアンケート一覧表示させてみる。 [PHP]

「PHP」でアンケート一覧表示させてみる。

■アンケート一覧表示確認。

       
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>アンケート一覧表示。</title>
        </head>
        <body>
        
      <?php ←「PHP」プログラムの開始。
        $dsn = 'mysql:dbname=phpbasic;host=localhost'; ←ローカルホストの「phpbasic」に。
        $user = 'root'; ←「root」権限で。
        $password = 'XXXXX;
        $dbh = new PDO($dsn,$user,$password); ←データベースに接続。
        $dbh->query('SET NAMES utf8'); ←命令内容記述。query('命令実行するSQL文');
        $sql = 'SELECT * FROM enquete WHERE 1'; ←「enquete」から全部。
        $stmt = $dbh->prepare($sql); ←「SQL文」で命令を出す準備。
        $stmt->execute(); ←データベースに「SQL文」を送信。「$stmt」に結果が返る。
        
      while(1) ←無限ループ。
        {
        $rec = $stmt->fetch(PDO::FETCH_ASSOC); ←1レコード取り出し。
        if($rec==false) ←取り出すレコードが「無くなったら」、
        {
        break; ←このループから脱出。
        }
        print $rec['code']; ←1レコード文の表示。
        print $rec['nickname']; ←1レコード文の表示。
        print $rec['email']; ←1レコード文の表示。
        print $rec['goiken']; ←1レコード文の表示。
        print '<br/>';
        }
      $dbh = null; ←データベース切断。
        ?> ←「PHP」終了。
      </body>
        </html>
      
       
      
26日、27日の記事と連動しています。 「ichiran.php」として「/var/www/htm/php」に保存。
「while(1)」(これより以前↑のプログラムの説明は先日分に) ループをするのですが(1)を付けることで無限ループになります。
「$rec = $stmt->fetch(PDO::FETCH_ASSOC);」で「$stmt」からデータを取り出します。「fetch」が順番に1レコードずつ取り出す命令。
「$stmt」に収まっているデータを「FETCH」により一つずつデータを取り出し、「$rec」に代入します。
1レコードは「code」「nickname」「email」「goiken」となります。
(詳細:http://www.phpbook.jp/tutorial/pdo/index7.html)

「if($rec==false)」はIF文で「もし~なら~する」ということですが、「==」は「同様なら」と言う意味で、「false」つまり「無」ならループから抜け出すという意味です。それまではループし、再び「$stmt」からデータを取り出し「$rec」を上書きします。
 ループから脱却する前に取り出したデータをフィールド名毎に「print」で表示させています。そしてループの初めに戻ります。

「$dbh = null;」によりデータベース切断します。

http://192.168.0.66/php/ichiran.php にアクセスすると下記のように表示されます。
1bibo-rokubibo-roku@yahoo.co.jpなななもはー
2magellanmagellan@yahoo.co.jpねむねむはー
3mekamochimekamochi@yahoo.co.jpこめたぬはー


■データ検索のページを作成してみる。(search.html)

       
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>データ検索</title>
        </head>
        
<body> <form method="post" action="search.php"> ご意見コードを入力してください。<br> <input name="code" type="text" style="width:100px"><br> <br> <input type="submit" value="送信"> </form>
</body> </html>


■データ検索のページを作成してみる。(search.php)

       
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>PHP基礎</title>
        </head>
        <body>
        
<?php $code=$_POST['code']; $dsn = 'mysql:dbname=phpbasic;host=localhost'; $user = 'root'; $password = 'XXXXX; $dbh = new PDO($dsn,$user,$password); $dbh->query('SET NAMES utf8'); $sql = 'SELECT * FROM enquete WHERE code='.$code; $stmt = $dbh->prepare($sql); $stmt->execute();
while(1) { $rec = $stmt->fetch(PDO::FETCH_ASSOC); if($rec==false) { break; } print $rec['code']; print $rec['nickname']; print $rec['email']; print $rec['goiken']; print '<br/>'; }
$dbh = null;
?>
</body> </html>
http://192.168.0.66/php/search.html
2[送信]にて下記表示されました。
2magellanmagellan@yahoo.co.jpねむねむはー
しかし
3 or 1[送信]にすると……
1bibo-rokubibo-roku@yahoo.co.jpなななもはー
2magellanmagellan@yahoo.co.jpねむねむはー
3mekamochimekamochi@yahoo.co.jpこめたぬはー
全部表示されてしまいました。これを「SQLインジェクション」と呼ぶ悪い行為だそうです。
これを禁止しましょう。
今回の場合「3 or 1」が「3 かもしくは全部のデータを出しなさい」という意味になります。「or 1」が「もしくは全部」になります。


■「SQLインジェクション」を阻止する。

       
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>PHP基礎</title>
        </head>
        
        <body>
        <?php
        $code=$_POST['code'];
        $dsn = 'mysql:dbname=phpbasic;host=localhost';
        $user = 'root';
        $password = 'maria';
        $dbh = new PDO($dsn,$user,$password);
        $dbh->query('SET NAMES utf8');
        $sql = 'SELECT * FROM enquete WHERE code=?'; ←変数の連結を止める。
        $stmt = $dbh->prepare($sql);
        $data[]=$code; ←新たな変数を使う。
        $stmt->execute($data); ←データを格納した変数を指定。
        
while(1) { $rec = $stmt->fetch(PDO::FETCH_ASSOC); if($rec==false) { break; } print $rec['code']; print $rec['nickname']; print $rec['email']; print $rec['goiken']; print '<br/>'; } $dbh = null; ?> </body> </html>
「$sql = 'SELECT * FROM enquete WHERE code='.$code;」を、変数の連結をやめて、データを入れたい部分を「?」で表現します。
「$sql = 'SELECT * FROM enquete WHERE code=?';」とします。
「$data[]=$code;」新たに変数を作りそこにデータを格納します。
「$stmt->execute($data);」SQL文で命令を出すときに、データを格納した変数を指定します。

http://192.168.0.66/php/search.php
3 or 1[送信]とすると
3mekamochimekamochi@yahoo.co.jpこめたぬはー
となります。これをプリペアードステートメントという書き方だそうです。


nice!(0)  コメント(10)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 10

Manuela

ある人が自分の必要なものを検索するとき、その人はそれを詳細に利用できるようにしたいので
そのことはここで維持されています。
by Manuela (2018-03-01 03:13) 

Tammi

あなたのスタイルは他の人と比べてユニークです
から。機会があれば投稿していただきありがとうございます。
私はちょうどこのウェブサイトをマーク予約すると思います。
by Tammi (2018-03-01 21:13) 

Willis

jeu de sexe adulte

This is the perfect webpage for anybody who wishes to find out about this
topic. You know a whole lot its almost hard to argue with you
(not that I actually will need to…HaHa). You definitely put
a fresh spin on a subject that has been discussed for years.
Wonderful stuff, just wonderful!
by Willis (2020-08-11 20:17) 

Claribel

And listen to the permissions you grant these apps. Hulu can’t afford to PAY its method.
Uploading videos is a great technique to share recollections and occasions with
family and mates. The one way to care about end users is to ensure
common access to every little thing on the internet.
And: "I’m saying that we can construct wealthy interactive net apps that work offline and in addition work when JavaScript fails or isn’t supported" (emphasis
added again). Till, that is, you realise that this permission permits
apps "to send your location information as much as 14,000 times per day, even when you’re not utilizing their apps." We
saw the size of this just lately, when the U.S.
Our open, nameless policy has allowed us to construct a video catalogue that
rivals even these who've been within the business for the longest time.
Each glossy journal archive is now a structured information set, and so is each video feed.
by Claribel (2020-09-04 00:17) 

Zane

Ꭼvery eekend i used to vіsit this website, as i wish for enjoyment, fοr the reason that this this weƅ site conatіons tuⅼy fastidious funnу
stuff too.
by Zane (2021-12-05 14:18) 

Shantell

Plеase let me know if you're looking for a writer fоr youг site.

You һhave some reаlly great articles and I ƅeliеve I would bе a good asset.
If you ever ᴡant to take some of thhe load off, I'd love to write some articles for
your blog in exchange for a link back to mine. Please shoot me an emaіl if interested.

Many thanks!
by Shantell (2022-03-13 07:52) 

Nola

Hey just wanted too ɡive you a quick hdads ᥙp and let you know a few of the images aren't loading properly.
I'm not sure why but I think its a linking issue.
I've tried it in two different web browsers and both show the ѕame results.
by Nola (2022-03-15 01:37) 

Joellen

What'ѕ up it's me, I am also vіsіtіng this web site daіly, this site is genuinely pleasant and tһe users are actually sharing nice thoughts.
by Joellen (2022-04-16 16:06) 

Wilton

I'm realⅼy enjoying the theme/design of your wеbsite.
Do you ever run into any wеb browser compatibiⅼіty issues?
A small number of my blog аuⅾience hɑve сomplained аbout my blog not
workinng correctly in Explorer but loߋks great in Safari.

Do you haѵe any solutions to help fix this issuе?
by Wilton (2022-05-19 11:44) 

Luis

I every time used to rread piece off ѡriting in new papers but now as Ι am a ᥙser of net so
from now Ι am using net for aгticles, thanks to web.
by Luis (2022-06-17 23:17) 

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。