teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

textareaのnameを単純なものにして、とりあえずデータを送信させようと修正しました。しかし、これでもデータが送信されておらず、困っております。

2023/04/12 09:15

投稿

shaketaku
shaketaku

スコア3

title CHANGED
File without changes
body CHANGED
@@ -16,6 +16,7 @@
16
16
  ```
17
17
  <!-----------カレンダープログラム--------------->
18
18
  <?php
19
+ var_dump($_POST['text_msg']); //追加
19
20
  $ym = null;
20
21
  $timestamp = null;
21
22
  $today = null;
@@ -80,26 +81,27 @@
80
81
  //str_repeat(文字列, 反復回数)
81
82
  $week .= str_repeat('<td></td>', $youbi);
82
83
 
83
- for($day = 1; $day <= $day_count; $day++, $youbi++){
84
+ for($day = 1; $day <= $day_count; $day++, $youbi++) {
84
85
 
85
86
  $date = $ym . '-' . $day;
86
87
  //それぞれの日付をY-m-d形式で表示例:2020-01-23
87
88
  //$dayはfor関数のおかげで1日づつ増えていく
88
89
 
89
- if($today == $date){
90
+ if($today == $date) {
90
91
  //もしその日が今日なら
91
- $week .= '<td class="today">' . $day . "<textarea type = 'text' name = 'text_msg{$day}'>" ;
92
+ $week .= '<td class="today">' . $day . "<textarea name = 'text_msg'>" ;
92
- }else{
93
+ } else {
93
94
  //上2つ以外なら
95
+ //修正
94
- $week .= '<td>' . $day . "<textarea type = 'text' name = 'text_msg{$day}'>";
96
+ $week .= '<td>' . $day . "<textarea name = 'text_msg'>";
95
97
  }
96
98
  $week .= '</textarea>'.'</td>';
97
99
 
98
- if($youbi % 7 == 6 || $day == $day_count){//週終わり、月終わりの場合
100
+ if($youbi % 7 == 6 || $day == $day_count) {//週終わり、月終わりの場合
99
101
  //%は余りを求める、||はまたは
100
102
  //土曜日を取得
101
103
 
102
- if($day == $day_count){//月の最終日、空セルを追加
104
+ if($day == $day_count) {//月の最終日、空セルを追加
103
105
  $week .= str_repeat('<td></td>', 6 - ($youbi % 7));
104
106
  }
105
107
 
@@ -128,54 +130,46 @@
128
130
  }
129
131
 
130
132
  if (!empty(($_POST['update']))) {
131
- //textareaの内容の空白削除
133
+  //修正
132
- for ($i =1; $i<=$day; $i++) {
133
- if ($_POST["text_msg{$day}"] === $_POST["text_msg{$i}"]) {
134
- $text_msg = preg_replace('/\A[\p{C}\p{Z}]++|[\p{C}\p{Z}]++\z/u', '', htmlspecialchars($_POST["text_msg{$day}"]));
134
+ $text_msg = preg_replace('/\A[\p{C}\p{Z}]++|[\p{C}\p{Z}]++\z/u', '',$_POST['text_msg']);
135
- echo "できた";
135
+ echo $text_msg;
136
- break;
136
+ if(empty($text_msg)){
137
+ $error_msg[] = '内容を入力してください。' ;
137
- } else {
138
+ }else{
138
- echo "できてないで";
139
+ $_SESSION['text_msg'] = $text_msg;
139
- }
140
140
  }
141
- }
142
141
 
142
+ if(empty($error_msg)){
143
+ $pdo -> beginTransaction();
143
144
 
144
- if(empty($text_msg)){
145
- $error_msg[] = '内容を入力してください。' ;
146
- }else{
145
+ try{
146
+ // SQL作成
147
- $_SESSION['text_msg'] = $text_msg;
147
+ $stmt = $pdo->prepare("INSERT INTO msg_box (text_msg) VALUES ( :text_msg)");
148
- }
149
148
 
150
- if(empty($error_msg)){
149
+ // 値をセット
151
- $pdo -> beginTransaction();
150
+ $stmt->bindParam( ':text_msg', $text_msg, PDO::PARAM_STR);
152
151
 
153
- try{
154
- // SQL作成
152
+ //sqlクエリの実行
155
- $stmt = $pdo->prepare("INSERT INTO msg_box (text_msg) VALUES ( :text_msg)");
153
+ $res = $stmt->execute();
156
154
 
157
- // 値をセット
158
- $stmt->bindParam( ':text_msg', $text_msg, PDO::PARAM_STR);
159
-
160
- //sqlクエリの実行
161
- $res = $stmt->execute();
162
-
163
- //コミット
155
+ //コミット
164
- $res = $pdo->commit();
156
+ $res = $pdo->commit();
165
- }catch(Exception $e){
157
+ }catch(Exception $e){
166
- $pdo -> rollBack();
158
+ $pdo -> rollBack();
159
+ }
160
+ if($res){
161
+ $_SESSION['success_msg'] = "書き込みに成功しました";
162
+ }else{
163
+ $error_msg[] = "書き込みに失敗しました。";
164
+ }
165
+ $stmt = null;
166
+ header('Location:[""]');
167
+ exit;
167
168
  }
168
- if($res){
169
- $_SESSION['success_msg'] = "書き込みに成功しました";
170
- }else{
169
+ }else{
171
- $error_msg[] = "書き込みに失敗しました。";
170
+ echo "データが送信されていない";
172
- }
171
+ }
173
- $stmt = null;
174
- header("Location:".$_SERVER['PHP_SELF']);
175
- exit;
176
- }
177
172
 
178
-
179
173
  if( !empty($pdo)){
180
174
 
181
175
  //メッセージのデータを取得する
@@ -242,7 +236,7 @@
242
236
  <body id="">
243
237
  <div class="container">
244
238
  <h3><a href="?ym=<?php echo $prev; ?>">&lt;</a><?php echo $html_title; ?><a href="?ym=<?php echo $next; ?>">&gt;</a></h3>
245
- <form action="<?php $_SERVER['PHP_SELF']; ?>" method = "POST">
239
+ <form action="" method = "POST">
246
240
  <table class="table table-bordered">
247
241
  <tr>
248
242
  <th>日</th>

1

ご指摘いただいたように、マークダウンの下にコードを描くように変更いたしました。教えていただき、ありがとうございます。

2023/04/11 15:07

投稿

shaketaku
shaketaku

スコア3

title CHANGED
File without changes
body CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
 
12
12
  ### 該当のソースコード
13
+
14
+
15
+ ソースコード
16
+ ```
13
17
  <!-----------カレンダープログラム--------------->
14
18
  <?php
15
19
  $ym = null;
@@ -264,8 +268,4 @@
264
268
  </html>
265
269
  ```ここに言語名を入力
266
270
 
267
- PHP mysql
268
271
 
269
- ソースコード
270
- ```
271
-