ご教授宜しくお願い致します。
phpで顧客管理画面を作成したいと思っています。
データベースに2つのテーブルを用意し、それぞれ顧客情報などを入力し
1つの画面でそれを組み合わせた表にしたいです。
具体的には、
cus1.php(氏名・電話番号・メモ)・・・テーブル「cm1」
cus2.php(利用日・コース・料金・担当)・・・テーブル[cm2]
cus1.phpにはAUTO_INCREMENT(顧客No)を設定し、
それをクリックするとcus3.phpが表示され、
一覧表示できる。といったものが作りたいです。
cus1.php
cus1.php
1<?php 2try{//DBに接続 3 $pdo = new PDO("mysql:host=localhost;dbname=cm;charset=utf8","root",""); 4 $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);//プリペアドステートメントのエミュレーションを無効に 5 6 $sql = 'SELECT * from cm1';//sql文の準備、cm1テーブルからデータを取得 7 8 $stmt = $pdo->query($sql);//sql文の実行 9 $stmt -> execute(); 10 11print '<table border =""> 12<th>顧客No.</th> 13<th>氏名</th> 14<th>電話番号</th> 15<th>MEMO</th>';//表のためのテーブルを作成 16 17 while($rec = $stmt->fetch(PDO::FETCH_ASSOC)){//クエリ結果を1行ずつ取得して連想配列として返す、をwhile文でループさせる 18 19 $customer = $rec['customer']; 20 $name = $rec['name']; 21 $tel = $rec['tel']; 22 $memo = $rec['memo']; 23 24 print '<tr>'; 25 print '<td align="center"><a href="cus3.php?customer=' . $customer . '">'. $customer .'</a></td>';//クリックするとcus3.phpに 26 print '<td align="center">'. $name .'</td>'; 27 print '<td align="center">'. $tel .'</td>'; 28 print '<td>'. $memo .'</td>'; 29 print '</tr>'; 30 } 31print '</table>'; 32 33}catch (PDOException $e) { 34 echo mb_convert_encoding($e->getMessage(),'UTF-8','SJIS-win'); 35} 36$pdo = null; 37?>
cus2.php
cus2.php
1<?php 2 3try{ 4 $pdo = new PDO("mysql:host=localhost;dbname=cm;charset=utf8","root",""); 5 $sql = 'SELECT * from cm2'; 6 7 $stmt = $pdo->query($sql); 8 $stmt -> execute(); 9 10print '<table border =1> 11<th>顧客No.</th> 12<th>利用日</th> 13<th>コース</th> 14<th>利用料金</th> 15<th>担当</th>'; 16 17 while($rec = $stmt->fetch(PDO::FETCH_ASSOC)){ 18 19 print '<tr>'; 20 print '<td align="center">'.$rec['customer'].'</td>'; 21 print '<td align="center">'.$rec['date'].'</td>'; 22 print '<td align="center">'.$rec['course'].'分</td>'; 23 print '<td align="center">'.number_format($rec['price']).'円</td>'; 24 print '<td align="center">'.$rec['charge'].'</td>'; 25 print '</tr>'; 26 } 27print '</table>'; 28 29}catch (PDOException $e) { 30 echo mb_convert_encoding($e->getMessage(),'UTF-8','SJIS-win'); 31} 32$pdo = null; 33?>
cus3.phpで選んだ顧客No情報を一覧として表示
cus3.php
cus3.php
1<?php 2 3$customer = $_GET['customer']; 4 5try{ 6 $pdo = new PDO("mysql:host=localhost;dbname=cm;charset=utf8","root","");//DB接続 7 $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);//プリペアドステートメントのエミュレーションを無効に 8 9 $sql = "SELECT * FROM cm1 JOIN cm2 ON cm1.customer = cm2.customer ORDER BY date ASC"; 10 $stmt =$pdo->prepare($sql); 11 $stmt -> execute(); 12 13while($result = $stmt->fetch(PDO::FETCH_ASSOC)){ 14 15 $name = ($result['name']); 16 $tel = ($result['tel']); 17 $date = ($result['date']); 18 $price = ($result['price']); 19 $charge = ($result['charge']); 20 $memo = ($result['memo']); 21 22 print '顧客No.' . $customer . '<br>';//顧客番号 23 print '名前:' . $name . '<br>';//名前 24 print '電話番号:' . $tel . '<br>';//電話番号 25 26print '<table border =""> 27<th>利用日</th> 28<th>利用金額</th> 29<th>担当</th> 30<th>MEMO</th>'; 31 32 print '<tr>'; 33 print '<td align="center">' . $date . '</td>';//日付 34 print '<td align="center">' . $price . '</td>';//金額 35 print '<td align="center">' . $charge . '</td>';//担当 36 print '<td>' . $memo . '</td>';//メモ 37 print '</tr>'; 38 } 39 print '</table>'; 40}catch (PDOException $e) { 41 echo mb_convert_encoding($e->getMessage(),'UTF-8','SJIS-win'); 42} 43 44$pdo = null; 45?>
cus3.phpの表は、
①顧客No.
②名前
③電話番号
表で④利用日⑤料金⑥担当⑦メモ
といった形にしたいです。
現状では、①の顧客No.をクリックするとテーブル内の①~⑦すべての項目が出てしまいます。
どう修正したらよいのかがわかりません。
顧客Noをクリックしたら該当の顧客情報しか出さないようにするにはどうしたらいいでしょうか?
アドバイスいただけましたら幸いです。
何卒、よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー