質問編集履歴
2
脱字
test
CHANGED
File without changes
|
test
CHANGED
@@ -98,4 +98,118 @@
|
|
98
98
|
---
|
99
99
|
$sql -> excute($_POST['check']);にした場合できるかなと思いましたが無理でした。
|
100
100
|
postにcheckの値が入っていれば送信して任意のものを消去できる,もしくはDBにもチェックボックスみたいな感じの役割のものを追記すればできると思いましたがわかりませんでした。有識者さんアドバイスお願いいたします。
|
101
|
+
|
102
|
+
---
|
103
|
+
**更新後のコード**
|
104
|
+
```PHP
|
105
|
+
<?php
|
106
|
+
session_start();
|
107
|
+
|
108
|
+
//DB接続//
|
109
|
+
try{
|
110
|
+
$pdo=new PDO('mysql:host=mysql154.phy.lolipop.lan;
|
111
|
+
dbname=LAA1290628-upup;charset=utf8',
|
112
|
+
'LAA1290628',
|
113
|
+
'Shion0724');
|
114
|
+
}catch(PDOException $e){
|
115
|
+
echo '接続できませんでした。';
|
116
|
+
}
|
117
|
+
?>
|
118
|
+
|
119
|
+
|
120
|
+
<!DOCTYPE html>
|
121
|
+
<html lang="en">
|
122
|
+
<head>
|
123
|
+
<meta charset="UTF-8">
|
124
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
125
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
126
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
|
127
|
+
<link rel="stylesheet" href="../css/style2.css">
|
128
|
+
<title>Document</title>
|
129
|
+
</head>
|
130
|
+
<body>
|
131
|
+
<?php
|
132
|
+
require('../header/header2.php');
|
133
|
+
?>
|
134
|
+
<hr>
|
135
|
+
<h1>リスト</h1>
|
136
|
+
<?php
|
137
|
+
echo '<form method="post">';
|
138
|
+
//セッションが入っている場合
|
139
|
+
if(isset($_SESSION['user'])){
|
140
|
+
echo 'ユーザーID:<input type="text" name="login_id" value="'.$_SESSION['user']['id'].'" readonly>','<br>';
|
141
|
+
}
|
142
|
+
|
143
|
+
//内容追加
|
144
|
+
echo '<label for="name">内容:</label><input type="text" name="contents" id="name" value="">';
|
145
|
+
echo '<button type="submit" name="add">追加</button>';
|
146
|
+
echo '</form>';
|
147
|
+
|
148
|
+
//内容消去
|
149
|
+
echo '<form id="check" method="post">';
|
150
|
+
echo '<button type="submit" name="delete">消去</button>';
|
151
|
+
if(isset($_POST['check'])){
|
152
|
+
$check = $_POST['check'];
|
153
|
+
foreach($check as $name){
|
154
|
+
//チェックボックスに内容が入っている
|
155
|
+
//echo $name;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
echo '</form>';
|
159
|
+
?>
|
160
|
+
|
161
|
+
<?php
|
162
|
+
//ログイン中のIDと書き込み内容をDBに登録//
|
163
|
+
if(isset($_SESSION['user']) && isset($_POST['add']) && !empty($_POST['contents'])){
|
164
|
+
$sql = $pdo->prepare('insert into text(login_id,contents) values(?,?)');
|
165
|
+
$sql -> bindvalue(1,$_POST['login_id']);
|
166
|
+
$sql -> bindvalue(2,$_POST['contents']);
|
167
|
+
$sql -> execute();
|
168
|
+
}
|
169
|
+
|
170
|
+
//内容を消去//
|
171
|
+
if(isset($_POST['delete'])){
|
172
|
+
$sql=$pdo->prepare('delete from text where login_id = "'.$_SESSION['user']['id'].'" and contents="'.$name.'"');
|
173
|
+
$sql -> bindvalue(1,$name);
|
174
|
+
$sql -> execute();
|
175
|
+
}
|
176
|
+
|
177
|
+
//ユーザーIDによって違う内容を表示//
|
178
|
+
if(isset($_SESSION['user']['id'])){
|
179
|
+
$sql=$pdo->prepare('select * from text where login_id = "'.$_SESSION['user']['id'].'"');
|
180
|
+
$sql->execute();
|
181
|
+
foreach($sql as $row){
|
182
|
+
// echo '<form method="post">';
|
183
|
+
echo $row['contents'];
|
184
|
+
echo '<input type="checkbox" name="check[]" form="check" value="'.$row['contents'].'" >';
|
185
|
+
echo '<br>';
|
186
|
+
// echo '</form>';
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
//PHP空白判定//
|
193
|
+
$add = filter_input(INPUT_POST,'add');
|
194
|
+
$delete = filter_input(INPUT_POST,'delete');
|
195
|
+
$contents = filter_input(INPUT_POST,'contents',);
|
196
|
+
$check = filter_input(INPUT_POST,'check',);
|
197
|
+
if(isset($add)){
|
198
|
+
if(empty($contents))
|
199
|
+
echo '内容を入力してください';
|
200
|
+
}
|
201
|
+
if(isset($delete)){
|
202
|
+
if(empty($check))
|
203
|
+
echo 'チェックを入れてください';
|
204
|
+
}
|
205
|
+
?>
|
206
|
+
|
207
|
+
|
208
|
+
</body>
|
209
|
+
</html>
|
210
|
+
|
211
|
+
```
|
212
|
+
|
213
|
+
現状ですと1つずつしか消去できない感じです。
|
214
|
+
delete文のところに問題があると思いますが躓いています。
|
101
215
|
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
### 実現したいこと
|
2
|
-
チェックボックスにチェックが入っているものだけをDBから消去したい
|
2
|
+
メモアプリを作成しています。チェックボックスにチェックが入っているものだけをDBから消去したい
|
3
3
|
#
|
4
4
|
```PHP
|
5
5
|
<?php
|