
仕様
①ユーザーにカレンダーのtextarea部分に情報を書き込み送信してもらう。
②同スクリプトにて受け取った情報をデータベースに入れる。
③書き込まれた情報をデータベースから取り出し各textareaに表示させる。
というものを考えています。
発生している問題・エラーメッセージ
自分は今上記の②の部分でつまずいています。
データベースに1つしか情報を送れていない。のが発生している問題です。
おそらくforや配列の部分がこの問題が起きている理由だと考えているのですが、どのようにコードを組めばうまくいくのかわからず質問させていただきました。
お忙しいところ恐縮ですが、ご回答いただけましたら幸いです。
該当のソースコード
php
1<?php 2$ym = null; 3$timestamp = null; 4$today = null; 5$html_title = null; 6$prev = null; 7$next = null; 8$day_count = null; 9$youbi = null; 10$weeks = null; 11$week = null; 12$day = null; 13$day_count = null; 14$option = null; 15$pdo = null; 16$message_array = array(); 17$text_msg = array(); 18$content = array(); 19//データベースの接続情報 20define( 'DB_HOST', 'localhost'); 21define( 'DB_USER', 'root'); 22define( 'DB_PASS', 'growry1293'); 23define( 'DB_NAME' , 'calender'); 24 25 26session_start(); 27 28try{ 29 $option = array( 30 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 31 PDO::MYSQL_ATTR_MULTI_STATEMENTS => false); 32 $pdo = new PDO('mysql:charset=UTF8;dbname='.DB_NAME.';host='.DB_HOST,DB_USER,DB_PASS, $option); 33}catch(PDOException $e){ 34 $error_msg[]= $e -> getMessage(); 35} 36 37if (isset(($_POST["update"]))) { 38 for($i = 0; $i<=30; $i++) { 39 if(!empty($_POST["text_msg"][$i])) { 40 $text_msg[$i]=preg_replace('/\A[\p{C}\p{Z}]++|[\p{C}\p{Z}]++\z/u', '', $_POST["text_msg"][$i]); 41 }else{ 42 $text_msg[$i] = null; 43 } 44 } 45 46 if(empty($error_msg)){ 47//ここで1つしか情報が送られていない。 48 49 try { 50 // 値をセット 51 for($i = 0; $i<=30; $i++){ 52 if(!empty($text_msg[$i])){ 53 // SQL作成 54 $stmt = $pdo->prepare("INSERT INTO msg_box (text_msg) VALUES ( :text_msg)"); 55 56 $stmt->bindParam(':text_msg', $text_msg[$i], PDO::PARAM_STR); 57 //sqlクエリの実行 58 $res = $stmt->execute(); 59 //コミット 60 $res = $pdo->commit(); 61 } 62 } 63 } catch(Exception $e) { 64 echo "$e"; 65 } 66 if($res) { 67 $_SESSION['success_msg'] = "書き込みに成功しました"; 68 } else { 69 $error_msg[] = "書き込みに失敗しました。"; 70 } 71 header("Location:./calender.php"); 72 exit; 73 } 74} 75 76if(!empty($pdo)) { 77 //メッセージのデータを取得する 78 $sql = "SELECT text_msg FROM msg_box"; 79 $message_array = $pdo->query($sql); 80} 81?> 82<!-----------カレンダープログラム---------------> 83 84<?php 85//タイムゾーンを設定 86date_default_timezone_set('Asia/Tokyo'); 87 88 89//前月・次月リンクが選択された場合は、GETパラメーターから年月を取得 90if(isset($_GET['ym'])){ 91 $ym = $_GET['ym']; 92}else{ 93 //今月の年月を表示 94 $ym = date('Y-m'); 95} 96 97//タイムスタンプ(どの時刻を基準にするか)を作成し、フォーマットをチェックする 98//strtotime('Y-m-01') 99$timestamp = strtotime($ym . '-01'); 100if($timestamp === false){//エラー対策として形式チェックを追加 101 //falseが返ってきた時は、現在の年月・タイムスタンプを取得 102 $ym = date('Y-m'); 103 $timestamp = strtotime($ym . '-01'); 104} 105 106//今月の日付 フォーマット 107$today = date('Y-m-j'); 108 109//カレンダーのタイトルを作成 例)2020年10月 110$html_title = date('Y年n月', $timestamp);//date(表示する内容,基準) 111 112//前月・次月の年月を取得 113//strtotime(,基準) 114$prev = date('Y-m', strtotime('-1 month', $timestamp)); 115$next = date('Y-m', strtotime('+1 month', $timestamp)); 116 117 118//該当月の日数を取得 119$day_count = date('t', $timestamp); 120 121//1日が何曜日か 122$youbi = date('w', $timestamp); 123 124//カレンダー作成の準備 125$weeks = []; 126$week = ''; 127 128//第1週目:空のセルを追加 129//str_repeat(文字列, 反復回数) 130$week .= str_repeat('<td></td>', $youbi); 131 132for($day = 1; $day <= $day_count; $day++, $youbi++) { 133 134 $date = $ym . '-' . $day; 135 //それぞれの日付をY-m-d形式で表示例:2020-01-23 136 //$dayはfor関数のおかげで1日づつ増えていく 137 138 if($today == $date) { 139 //もしその日が今日なら 140 $week .= '<td class="today">' . $day . "<textarea name = 'text_msg[]'>"; 141 }else { 142 //上2つ以外なら 143 $week .= '<td>' . $day . "<textarea name = 'text_msg[]'>"; 144 } 145 if(!empty($text_msg[$day])){ 146//ここでも情報が取得できていない。 147 foreach( $message_array as $value){ 148 $week .= $value.'</textarea>'.'</td>'; 149 } 150 }else{ 151 $week .= '</textarea>'.'</td>'; 152 } 153 154 if($youbi % 7 == 6 || $day == $day_count) {//週終わり、月終わりの場合 155 //%は余りを求める、||はまたは 156 //土曜日を取得 157 158 if($day == $day_count) {//月の最終日、空セルを追加 159 $week .= str_repeat('<td></td>', 6 - ($youbi % 7)); 160 } 161 162 $weeks[] = '<tr>' . $week . '</tr>'; //weeks配列にtrと$weekを追加 163 164 $week = '';//weekをリセット 165 } 166} 167 168?> 169 170 171<!-----------カレンダープログラム---------------> 172<!-----------データベースに接続--------> 173 174 175<!DOCTYPE html> 176<html lang="ja"> 177<head> 178 <meta charset="utf-8"> 179 <title>PHPカレンダー</title> 180 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 181 <link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet"> 182 <style> 183 .container { 184 font-family: 'Noto Sans', sans-serif; 185 margin-top: 80px; 186 width: 80vw; 187 margin: 0 auto; 188 } 189 h3 { 190 margin-bottom: 30px; 191 } 192 th { 193 height: 30px; 194 text-align: center; 195 } 196 td { 197 height: 100px; 198 } 199 textarea{ 200 height: 100px; 201 width: auto; 202 } 203 .today { 204 background: orange; 205 } 206 th:nth-of-type(1), td:nth-of-type(1) { 207 color: red; 208 } 209 th:nth-of-type(7), td:nth-of-type(7) { 210 color: blue; 211 } 212 .holiday{ 213 color: red; 214 } 215 .update_box{ 216 text-align: center; 217 } 218 .submit_box{ 219 width: 120px; 220 height: 50px; 221 margin: 50px auto; 222 } 223 </style> 224</head> 225<body id=""> 226 <div class="container"> 227 <h3><a href="?ym=<?php echo $prev; ?>"><</a><?php echo $html_title; ?><a href="?ym=<?php echo $next; ?>">></a></h3> 228 <form action="<?=$_SERVER['SCRIPT_NAME']?>" method="POST"> 229 <table class="table table-bordered"> 230 <tr> 231 <th>日</th> 232 <th>月</th> 233 <th>火</th> 234 <th>水</th> 235 <th>木</th> 236 <th>金</th> 237 <th>土</th> 238 </tr> 239 <?php 240 foreach ($weeks as $week) { 241 echo $week; 242 } 243 ?> 244 </table> 245 <div class = "update_box"> 246 <input type="submit" class = "submit_box" value = "更新する" name = "update"></input> 247 </div> 248 </form> 249 </div> 250</body> 251</html>

