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

質問編集履歴

2

エラー内容

2019/07/23 01:43

投稿

kumo2kumo
kumo2kumo

スコア13

title CHANGED
File without changes
body CHANGED
@@ -23,8 +23,9 @@
23
23
  }
24
24
 
25
25
  $idx = 0;
26
+ $sql = '';
26
27
  while ($idx < count($arr)) {
27
- $sql ='INSERT INTO テーブル名 (id,word) VALUES (NULL,$arr[$idx])';
28
+ $sql .='INSERT INTO テーブル名 (id,word) VALUES (NULL,$arr[$idx])';
28
29
  $stmt = $pdo->prepare($sql);
29
30
  $status = $stmt->execute();
30
31
  $idx = $idx + 1;
@@ -35,6 +36,12 @@
35
36
  } else {
36
37
  header('Location: index.php');
37
38
  ```
39
+ エラー内容
40
+ sqlError:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[$idx])' at line 1
41
+
42
+
43
+
44
+
38
45
  ### 補足情報(FW/ツールのバージョンなど)
39
46
 
40
47
  超基礎的ですいません、「mysql php 配列」といったワードで検索しましたがよくわからず…

1

検索ワードの追記、コードの追記

2019/07/23 01:43

投稿

kumo2kumo
kumo2kumo

スコア13

title CHANGED
File without changes
body CHANGED
@@ -16,14 +16,25 @@
16
16
 
17
17
  ここに問題に対して試したことを記載してください。
18
18
  ```php
19
+ try {
20
+ $pdo = new PDO($dbn, $user, $pwd);
21
+ } catch (PDOException $e) {
22
+ exit('dbError:'.$e->getMessage());
23
+ }
24
+
19
25
  $idx = 0;
20
- while ($idx < count($array)) {
26
+ while ($idx < count($arr)) {
21
- $sql ='INSERT INTO テーブル名(id, word) VALUES(NULL, $arr[$idx])';
27
+ $sql ='INSERT INTO テーブル名 (id,word) VALUES (NULL,$arr[$idx])';
28
+ $stmt = $pdo->prepare($sql);
29
+ $status = $stmt->execute();
22
30
  $idx = $idx + 1;
23
- }
31
+
24
- $stmt = $pdo->prepare($sql);
32
+ if ($status==false) {
25
- $status = $stmt->execute();
33
+ $error = $stmt->errorInfo();
34
+ exit('sqlError:'.$error[2]);
35
+ } else {
36
+ header('Location: index.php');
26
37
  ```
27
38
  ### 補足情報(FW/ツールのバージョンなど)
28
39
 
29
- 超基礎的ですいません、調べてもよくわからず…
40
+ 超基礎的ですいません、「mysql php 配列」といったワードで検索しましたがよくわからず…