###PHPからMySQLでわからないところがあります。
タメ語と正しい敬語どちらかに含まれる文字を検索出来るようにしたいのですが、上手く機能しません。
ご教示ください。
php:■honorific_output.php
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xm1ns="http://www.w3.org/1999/xhtml"> 4<html lang="ja"> 5<head> 6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 7<meta http-equiv="Content-Script-Type" content="text/javascript"> 8 9 10<title>PHP入門</title> 11<style type="text/css"> 12.font1 { 13 font-family: "あずきフォント", serif; 14} 15 16.font2 { 17 font-family: "あずきフォント", serif; 18 font-size: 160%; 19 color: "EEEEEE"; 20} 21</style> 22</head> 23<body> 24 </br> 25<!-- <table border="1" width="80%"> --> 26<table> 27 <tr style="background: #ccccff"> 28 <th class="font1">番号</th> 29 <th class="font1">タメ語</th> 30 <th class="font1">正しい敬語</th> 31 <th class="font1">例文</th> 32 </tr> 33 <?php 34 $pdo = new PDO ( 'mysql:host=localhost;dbname=honorific;charset=utf8', 'staff', 'password' ); 35 $sql = $pdo->prepare ( 'select * from honorifics where wrongHonorific like ? or rightHonorific like ?'); 36 $sql->execute (array ('%'.$_REQUEST ['keyword'].'%','%'.$_REQUEST['keyword'].'%')); 37// $sql->execute(['%'.$_REQUEST['keyword'].'%']); 38 foreach ( $sql->fetchAll () as $row ) { 39 echo '<tr>'; 40 echo '<td>', $row ['id'], '</td>'; 41 echo '<td>', $row ['wrongHonorific'], '</td>'; 42 echo '<td>', $row ['rightHonorific'], '</td>'; 43 echo '<td>', $row ['example'], '</td>'; 44 echo '</tr>'; 45 } 46 ?> 47 </table> 48</body> 49</html>
php:■honorific_input.php
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xm1ns="http://www.w3.org/1999/xhtml"> 4<html lang="ja"> 5<head> 6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 7<meta http-equiv="Content-Script-Type" content="text/javascript"> 8 9 10<title>敬語学習ノート</title> 11<style type="text/css"> 12.font1 { 13 font-family: "あずきフォント", serif; 14} 15 16.font2 { 17 font-family: "あずきフォント", serif; 18 font-size:160%; 19 color:"EEEEEE"; 20} 21.body{ 22 cursor:url(hosi5.ani); 23} 24html,body { 25 scrollbar-base-color:#c3d7e0; 26 scrollbar-track-color:#c3d7e0; 27 scrollbar-face-color:#c3d7e0; 28 scrollbar-shadow-color:#c3d7e0; 29 scrollbar-darkshadow-color:#c3d7e0; 30 scrollbar-highlight-color:#c3d7e0; 31 scrollbar-3dlight-color:#c3d7e0; 32 scrollbar-arrow-color:#ffffff; 33 filter:chroma(color=#c3d7e0); 34 } 35</style> 36</head> 37<body> 38 <table border="1" width="80%"> 39 <caption class="font2" >敬語学習ノート</caption> 40</br> 41<p class="font1">検索したいタメ語を入力してください</p> 42<form action="" method="post"> 43<input type="text" name="keyword"> 44<input type="submit" value="検索"> 45</form> 46</p> 47 48 </br> 49 <tr style="background: #ccccff"> 50 <th class="font1">番号</th> 51 <th class="font1">タメ語</th> 52 <th class="font1">正しい敬語</th> 53 <th class="font1">例文</th> 54 </tr> 55<?php 56 57$pdo = new PDO ( 'mysql:host=localhost;dbname=honorific;charset=utf8', 'staff', 'password' ); 58 59if($_POST){ 60$vKeyword = isset($_POST['keyword']) ? $_POST['keyword']:''; 61 62$sql = $pdo->prepare ( 'select * from honorifics where wrongHonorific like ?' ); 63$sql->execute (array ('%'.$vKeyword.'%')); 64function h($str) { 65 return htmlspecialchars ( $str ); 66} 67foreach ( $sql->fetchAll() as $row ) { 68 echo '<tr>'; 69 echo '<td>' . h ( $row ['id'] ), '</td>'; 70 echo '<td>', h ( $row ['wrongHonorific'] ), '</td>'; 71 echo '<td>', h ( $row ['rightHonorific'] ), '</td>'; 72 echo '<td>', h ( $row ['example'] ), '</td>'; 73 echo '</tr>'; 74 echo "\n"; 75} 76}else if(empty($_POST['keyword'])){ 77function h($str) { 78 return htmlspecialchars ( $str ); 79} 80foreach ( $pdo->query('select * from honorifics') as $row ) { 81 echo '<tr>'; 82 echo '<td>' . h ( $row ['id'] ), '</td>'; 83 echo '<td>', h ( $row ['wrongHonorific'] ), '</td>'; 84 echo '<td>', h ( $row ['rightHonorific'] ), '</td>'; 85 echo '<td>', h ( $row ['example'] ), '</td>'; 86 echo '</tr>'; 87 echo "\n"; 88} 89} 90?> 91</table> 92</body> 93</html>
回答1件
あなたの回答
tips
プレビュー