前提
phpとデータベースをphpmyadmin5.1.1で簡易的なカレンダーアプリを作っています。
選択した日付の横のtdに送信したテキストを表示させる機能を実装中に以下のが発生しました。
実現したいこと
登録を押した時の年($year)と月($month)と日にち($d)が、DB内のyearとmonthとdayとと同じだった場合に処理が実行されるようにしたい
発生している問題・エラーメッセージ
全ての日付の横のtdにテキストが表示されてしまう。
該当のソースコード
php
1<?php 2 3$year = date("Y"); 4$month = date("m"); 5$answer = ""; 6$datehide = ""; 7$data = ""; 8$text =""; 9$redday=""; 10$redmonth =""; 11$redyear = ""; 12$redtext = ""; 13if(isset($_GET['open'])){ 14 $year = $_GET['year']; 15 $month = $_GET['month']; 16 17 if($year >= 2031 || $year <= 1999){ 18 $answer = "無効な年 "; 19 $redyear = "borderred"; 20 $year = date("Y"); 21 $datehide = "datehide"; 22 } 23 24 if($month >= 13 || $month <= 0){ 25 $answer .= "無効な月 "; 26 $redmonth = "borderred"; 27 $month = date("m"); 28 $datehide = "datehide"; 29 } 30 31} 32 33if(isset($_POST['add'])){ 34 $answer = ""; 35 $redmonth =""; 36 $redyear = ""; 37 $day = $_POST['day']; 38 $text = $_POST['text']; 39 $date = date('d',strtotime("$year-$month last day of this month")); 40 if($day > $date || $day <= 0){ 41 $answer .= "無効な日付 "; 42 $redday = "borderred"; 43 $datehide = "datehide"; 44 }elseif($text == "" || ctype_space($text)||mb_ereg_match(' ',$text)){ 45 $answer .= "空欄 "; 46 $redtext = "borderred"; 47 }else{ 48 $dsn = 'mysql:dbname=schedule;host=localhost'; 49 $user = 'root'; 50 $pass = ''; 51 52 try{ 53 $dbh = new PDO($dsn, $user, $pass); 54 55 $dbh->query('SET NAMES utf8'); 56 57 58 $sql = 'insert into schedule (year, month,day,content) values (?, ?, ?, ?)'; 59 $stmt = $dbh->prepare($sql); 60 $flag = $stmt->execute(array($year, $month,$day,$text)); 61 62 }catch (PDOException $e){ 63 print('Error:'.$e->getMessage()); 64 die(); 65 } 66 $dbh = null; 67 header("Location:calendar2.php?year=$year&month=$month&open=表示"); 68 exit; 69 } 70 71} 72 73?> 74 75<html> 76 <style> 77 tbody{ 78 display:flex; 79 } 80 tr{ 81 display:flex; 82 border:1px solid black; 83 } 84 85 td{ 86 border:1px solid black; 87 display:flex; 88 } 89 90 .datehide{ 91 display:none; 92 } 93 94 .borderred{ 95 border:3px solid red; 96 } 97 </style> 98 <form action="" method="get"> 99 <select name="year" class="<?php echo $redyear ?>"> 100 <?php 101 if($year == ""){ 102 $year = date("Y"); 103 104 }else{ 105 $year = $year; 106 } 107 for($y= 2000; $y <=2030; $y++){ 108 if($year == $y){ 109 $selected = "selected"; 110 }else{ 111 $selected = ""; 112 } 113 echo "<option $selected>$y</option>"; 114 } 115 ?> 116 </select> 117 <select name="month" id="" class="<?php echo $redmonth ?>"> 118 <?php 119 if($month == ""){ 120 $month = date("m"); 121 122 }else{ 123 $month = $month; 124 } 125 for($m= 1; $m <=12; $m++){ 126 if($month == $m){ 127 $selected = "selected"; 128 }else{ 129 $selected = ""; 130 } 131 echo "<option $selected>$m</option>"; 132 } 133 ?> 134 </select> 135 <input type="submit" name="open" value="表示" ></input> 136 </form> 137 <br></br> 138 <div class="<?php echo $datehide ?>"><?php echo $year?>年<?php echo $month ?>月</div> 139 <?php echo $answer?> 140 <form action="" method="post"> 141 <select name="day" id="" class="<?php echo $redday ?>"> 142 <?php 143 $date = date('d',strtotime("$year-$month last day of this month")); 144 for($d= 1; $d <=$date; $d++){ 145 echo "<option>$d</option>"; 146 } 147 ?> 148 </select> 149 <input type="text" name="text" class="<?php echo $redtext ?>"> 150 <input type="submit" name="add" value="登録"> 151 <?php 152 for($d= 1; $d <=$date; $d++){ 153 ?> 154 <table> 155 <tbody> 156 <tr> 157 <?php echo "<td>$d</td>"; ?> 158 <td> 159 <?php 160 $txt =""; 161 $dsn = 'mysql:dbname=schedule;host=localhost'; 162 $user = 'root'; 163 $pass = ''; 164 165 $dbh = new PDO($dsn, $user, $pass); 166 167 // SQLを実行する 168 $sql = 'SELECT * FROM schedule'; 169 $stmt = $dbh->query($sql); 170 // SQLの実行結果を取り出す 171 while($schedule = $stmt->fetch(PDO::FETCH_ASSOC)){ 172 /*selectで値を抽出してwhereで登録した年($year)と月($month)と日にち($d)が 173 DB内のyearとmonthとdayとと同じだった場合に処理が実行されるように記述した 174 つもりだが全てのtdに$textが表示されてしまう。*/ 175 if("SELECT year, month, day FROM schedule WHERE year = $year AND month = $month AND day = $d"){ 176 if($txt == ""){ 177 $txt = htmlspecialchars($schedule["content"], ENT_QUOTES, "UTF-8"); 178 }else{ 179 $txt .= htmlspecialchars(",".$schedule["content"], ENT_QUOTES, "UTF-8"); 180 } 181 } 182 } 183 echo $txt; 184 // 接続を閉じる 185 $dbh = null; 186 187 ?> 188 189 </td> 190 </tr> 191 </tbody> 192 </table> 193 <?php 194 } 195 ?> 196 </form> 197</html>
試したこと
条件を
if("SELECT year, month, day FROM schedule WHERE year IN($year) AND month IN($month) AND day IN($d"))
にしてみたが動かない
回答2件
あなたの回答
tips
プレビュー