質問編集履歴

4

2022/08/01 02:08

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -47,11 +47,11 @@
47
47
 
48
48
  <div>
49
49
  <?php
50
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
50
+ $pdo=new PDO('mysql:host=localhost;dbname=;charset=utf8',
51
51
  'root', '');
52
52
  $sql=$pdo->prepare('select * from images where file_name=?');
53
53
  echo '<table>';
54
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
54
+ $pdo=new PDO('mysql:host=localhost;dbname=;charset=utf8',
55
55
  'root', '');
56
56
  if (isset($_REQUEST['keyword'])) {
57
57
  $sql=$pdo->prepare('select * from images where file_name like ?');
@@ -110,12 +110,12 @@
110
110
  <?php session_start(); ?>
111
111
 
112
112
  <?php
113
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
113
+ $pdo=new PDO('mysql:host=localhost;dbname=;charset=utf8',
114
114
  'root', '');
115
115
  $sql=$pdo->prepare('select * from images ');
116
116
  echo '<table>';
117
117
  echo '<th>写真名</th><th>写真</th>';
118
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
118
+ $pdo=new PDO('mysql:host=localhost;dbname=charset=utf8',
119
119
  'root', '');
120
120
  if (isset($_REQUEST['keyword'])) {
121
121
  $sql=$pdo->prepare('select * from images where file_name like ?');

3

合ってるかわかりませんが、やってみました

2022/07/31 14:21

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -11,58 +11,175 @@
11
11
 
12
12
  ```
13
13
  エラーメッセージ
14
- ```Warning: Undefined array key "id" in C:\xampp\htdocs\php\kouka2249576\select_output.php on line 16
15
-
16
- Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\php\kouka2249576\select_output.php:16 Stack trace: #0 C:\xampp\htdocs\php\kouka2249576\select_output.php(16): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\php\kouka2249576\select_output.php on line 16
17
-
14
+ ```エラーは出てないんですけど、古い順が反映されないです
18
15
 
19
16
  ### 該当のソースコード
20
17
 
21
18
  ```itiran.php
22
19
 
20
+ <!DOCTYPE html>
21
+ <html lang="ja">
22
+ <head>
23
+ <meta charset="UTF-8">
24
+ <title>一覧</title>
25
+ <meta name="description" content="画像ファイルをアップロードします。">
26
+ <link rel="stylesheet" href="style.css">
27
+ <style>
28
+ .uploadsl {
29
+ height: 100px;
30
+ }
31
+ </style>
32
+ </head>
33
+ <body>
34
+ <?php session_start(); ?>
35
+ <?php require '../header.php'; ?>
36
+ <?php require 'menu.php'; ?>
23
- <form action="select_output.php" method = "POST">
37
+ <form action="select_output.php" method = "POST">
24
- <select name= "zyun">
38
+ <select name= "zyun">
25
- <option value = "新しい順">新しい順</option>
39
+ <option value = "新しい順">新しい順</option>
26
- <option value = "古い順">古い順</option>
40
+ <option value = "古い順">古い順</option>
27
- </select>
41
+ </select>
28
- <input type="submit"name="submit"value="送信"/>
42
+ <input type="submit"name="submit"value="送信"/>
29
43
 
30
44
 
31
45
 
32
- select_output.php
46
+ <hr>
33
47
 
48
+ <div>
34
- <?php
49
+ <?php
35
- if(isset($_POST["zyun"])) {
36
- // セレクトボックスで選択された値を受け取る
37
- $fruit = $_POST["zyun"];
38
-
39
- // 受け取った値を画面に出力
40
- echo $fruit;
41
- switch ($_REQUEST['zyun']) {
42
- case '新しい順':
43
- if (isset($_SESSION['customer'])) {
44
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
50
+ $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
45
- 'root', '');
51
+ 'root', '');
52
+ $sql=$pdo->prepare('select * from images where file_name=?');
53
+ echo '<table>';
54
+ $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
55
+ 'root', '');
56
+ if (isset($_REQUEST['keyword'])) {
46
- $sql=$pdo->prepare('SELECT * FROM `images` ORDER BY `images`.`uploaded_on` ASC');
57
+ $sql=$pdo->prepare('select * from images where file_name like ?');
47
- $sql->execute([$_SESSION['id']]);
58
+ $sql->execute(['%'.$_REQUEST['keyword'].'%']);
59
+ } else {
60
+ $sql=$pdo->prepare('select * from images');
61
+ $sql->execute([]);
48
62
  }
49
-
63
+ foreach ($sql as $row) {
50
-
64
+ $id=$row['id'];
65
+ echo '<tr>';
66
+ echo '<td>','<a href="syousai.php?id=', $id, '">',$row['file_name'],'</a>','</td>';
67
+ echo '<td>';
68
+ echo '<img class="uploadsl" src="./uploads/', $row['file_name'], '">';
69
+ echo '</td>';
70
+ echo '</tr>';
71
+ }echo '</table>';
72
+
73
+
74
+ // データベースから画像を取得する
75
+ $query = $pdo->query("SELECT * FROM images ORDER BY uploaded_on DESC");
76
+
77
+ if($query > 0){
78
+ while($row = $query->fetch_assoc()){
79
+ $imageURL = 'uploads/'.$row["file_name"];
80
+ ?>
81
+ <img src="<?php echo $imageURL; ?>" alt="" />
82
+ <?php }
83
+
84
+
85
+ }else{ ?>
86
+
87
+ <?php } ?>
88
+ <?php require '../footer.php'; ?>
89
+ </body>
90
+ </html>
91
+
92
+
93
+ select_output
94
+
95
+ <!DOCTYPE html>
96
+ <html lang="ja">
97
+ <head>
98
+ <meta charset="UTF-8">
99
+ <title>一覧</title>
100
+ <meta name="description" content="画像ファイルをアップロードします。">
101
+ <link rel="stylesheet" href="style.css">
102
+ <style>
103
+ .uploadsl {
104
+ height: 100px;
105
+ }
106
+ </style>
107
+ </head>
108
+ <body>
109
+
110
+ <?php session_start(); ?>
111
+
112
+ <?php
113
+ $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
114
+ 'root', '');
115
+ $sql=$pdo->prepare('select * from images ');
116
+ echo '<table>';
117
+ echo '<th>写真名</th><th>写真</th>';
118
+ $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
119
+ 'root', '');
120
+ if (isset($_REQUEST['keyword'])) {
121
+ $sql=$pdo->prepare('select * from images where file_name like ?');
122
+ $sql->execute(['%'.$_REQUEST['keyword'].'%']);
123
+ } else {
124
+ $sql=$pdo->prepare('select * from images');
125
+ $sql->execute([]);
126
+ }
127
+ foreach ($sql as $row) {
128
+ $id=$row['id'];
129
+ echo '<tr>';
130
+ echo '<td>','<a href="syousai.php?id=', $id, '">',$row['file_name'],'</a>','</td>';
131
+ echo '<td>';
132
+ echo '<img class="uploads" src="./uploads/', $row['file_name'], '">';
133
+ echo '</td>';
134
+ echo '</tr>';
135
+ }echo '</table>';
136
+
137
+ if(isset($_POST["zyun"])) {
138
+ // セレクトボックスで選択された値を受け取る
139
+ $fruit = $_POST["zyun"];
140
+
141
+ // 受け取った値を画面に出力
142
+ echo $fruit;
143
+ switch ($_REQUEST['zyun']) {
144
+ case '新しい順':
145
+ $query = $pdo->query("SELECT * FROM images ORDER BY uploaded_on DESC");
146
+
147
+ if($query > 0){
148
+ while($row = $query->fetch_assoc()){
149
+ $imageURL = 'uploads/'.$row["file_name"];
150
+ ?>
151
+ <img src="<?php echo $imageURL; ?>" alt="" />
152
+ <?php }
153
+
154
+
155
+ }else{
156
+
157
+ }
158
+
51
- break;
159
+ break;
52
- case '古い順':
160
+ case '古い順':
53
- if (isset($_SESSION['customer'])) {
54
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
55
- 'root', '');
56
- $sql=$pdo->prepare('SELECT * FROM `images` ORDER BY `images`.`uploaded_on` DESC');
161
+ $query = $pdo->query("SELECT * FROM `images` ORDER BY uploaded_on ASC");
162
+
163
+ if($query > 0){
164
+ while($row = $query->fetch_assoc()){
57
- $sql->execute([$_SESSION['customer']['id'], $_REQUEST['id']]);
165
+ $imageURL = 'uploads/'.$row["file_name"];
166
+ ?>
167
+ <img src="<?php echo $imageURL; ?>" alt="" />
168
+ <?php }
169
+
170
+
171
+ }else{
172
+
173
+ }
174
+
175
+ break;
58
176
  }
59
-
60
- break;
177
+
61
- }
178
+ }
62
-
63
- }
64
- require 'okini.php';
65
- ?>
179
+ ?>
66
180
 
67
- <?php require '../footer.php'; ?>
181
+ <?php require '../footer.php'; ?>
182
+ </body>
183
+ </html>
68
184
 
185
+

2

変更しました

2022/07/28 22:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,5 @@
1
1
  ### 前提
2
2
 
3
- ここに質問の内容を詳しく書いてください。
4
3
  phpで写真をセレクトボックスで新しい順、古い順でやってみたところ、よくわからなかったので、詳しく教えてください。
5
4
  myadmin です
6
5
 

1

長かったので、短くしました。

2022/07/28 21:44

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -21,24 +21,6 @@
21
21
 
22
22
  ```itiran.php
23
23
 
24
- <!DOCTYPE html>
25
- <html lang="ja">
26
- <head>
27
- <meta charset="UTF-8">
28
- <title>一覧</title>
29
- <meta name="description" content="画像ファイルをアップロードします。">
30
- <link rel="stylesheet" href="style.css">
31
- <style>
32
- .uploads {
33
- height: 500px;
34
- }
35
- </style>
36
- </head>
37
- <body>
38
- <?php require '../header.php'; ?>
39
- <?php require 'menu.php'; ?>
40
- <input type="text" name="keyword">
41
- <input type="submit" value="検索">
42
24
  <form action="select_output.php" method = "POST">
43
25
  <select name= "zyun">
44
26
  <option value = "新しい順">新しい順</option>
@@ -48,58 +30,7 @@
48
30
 
49
31
 
50
32
 
51
- <hr>
52
-
53
- <div>
54
- <?php
55
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
56
- 'root', '');
57
- $sql=$pdo->prepare('select * from images where file_name=?');
58
- echo '<table>';
59
- echo '<th>写真名</th><th>写真</th>';
60
- $pdo=new PDO('mysql:host=localhost;dbname=kouka2249576;charset=utf8',
61
- 'root', '');
62
- if (isset($_REQUEST['keyword'])) {
63
- $sql=$pdo->prepare('select * from images where file_name like ?');
64
- $sql->execute(['%'.$_REQUEST['keyword'].'%']);
65
- } else {
66
- $sql=$pdo->prepare('select * from images');
67
- $sql->execute([]);
68
- }
69
- foreach ($sql as $row) {
70
- $id=$row['id'];
71
- echo '<tr>';
72
- echo '<td>','<a href="syousai.php?id=', $id, '">',$row['file_name'],'</a>','</td>';
73
- echo '<td>';
74
- echo '<img class="uploads" src="./uploads/', $row['file_name'], '">';
75
- echo '</td>';
76
- echo '</tr>';
77
- }echo '</table>';
78
-
79
-
80
- // データベースから画像を取得する
81
- $query = $pdo->query("SELECT * FROM images ORDER BY uploaded_on DESC");
82
-
83
- if($query > 0){
84
- while($row = $query->fetch_assoc()){
85
- $imageURL = 'uploads/'.$row["file_name"];
86
- ?>
87
- <img src="<?php echo $imageURL; ?>" alt="" />
88
- <?php }
89
-
90
-
91
- }else{ ?>
92
-
93
- <?php } ?>
94
- <?php require '../footer.php'; ?>
95
- </div>
96
- </body>
97
- </html>
98
-
99
-
100
33
  select_output.php
101
-
102
- <?php session_start(); ?>
103
34
 
104
35
  <?php
105
36
  if(isset($_POST["zyun"])) {
@@ -136,85 +67,3 @@
136
67
 
137
68
  <?php require '../footer.php'; ?>
138
69
 
139
- syasin.sql
140
-
141
- -- phpMyAdmin SQL Dump
142
- -- version 5.1.1
143
- -- https://www.phpmyadmin.net/
144
- --
145
- -- ホスト: 127.0.0.1
146
- -- 生成日時: 2022-02-14 14:36:45
147
- -- サーバのバージョン: 10.4.22-MariaDB
148
- -- PHP のバージョン: 7.4.27
149
-
150
- SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
151
- START TRANSACTION;
152
- SET time_zone = "+00:00";
153
-
154
-
155
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
156
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
157
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
158
- /*!40101 SET NAMES utf8mb4 */;
159
-
160
- --
161
- -- データベース: `kouka`
162
- create database syasin default character set utf8 collate utf8_general_ci;
163
- use syasin;
164
-
165
- -- --------------------------------------------------------
166
-
167
- --
168
- -- テーブルの構造 `subject`
169
- --
170
-
171
- create table product (
172
- id int auto_increment primary key,
173
- name varchar(200) not null,
174
- price int not null
175
- );
176
-
177
- create table customer (
178
- id int auto_increment primary key,
179
- name varchar(100) not null,
180
- address varchar(200) not null,
181
- login varchar(100) not null unique,
182
- password varchar(100) not null
183
- );
184
-
185
- create table purchase (
186
- id int not null primary key,
187
- customer_id int not null,
188
- foreign key(customer_id) references customer(id)
189
- );
190
-
191
- create table purchase_detail (
192
- purchase_id int not null,
193
- product_id int not null,
194
- count int not null,
195
- primary key(purchase_id, product_id),
196
- foreign key(purchase_id) references purchase(id),
197
- foreign key(product_id) references images(id)
198
- );
199
-
200
- create table favorite (
201
- customer_id int not null,
202
- syasin_id int not null,
203
- primary key(customer_id, syasin_id),
204
- foreign key(customer_id) references customer(id),
205
- foreign key(syasin_id) references images(id)
206
- );
207
-
208
- CREATE TABLE `images` (
209
- `id` int(11) NOT NULL AUTO_INCREMENT,
210
- `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
211
- `uploaded_on` datetime NOT NULL,
212
- `status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
213
- PRIMARY KEY (`id`)
214
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
215
-
216
- insert into customer values(null, '熊木 和夫', '東京都新宿区西新宿2-8-1', 'kumaki', 'BearTree1');
217
- ```
218
-
219
-
220
-