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

質問編集履歴

1

文字追加

2021/11/12 10:00

投稿

alyssa703957
alyssa703957

スコア12

title CHANGED
File without changes
body CHANGED
@@ -36,6 +36,84 @@
36
36
  xhr.send(formData);
37
37
  }
38
38
  ```
39
+ insert-img-chat.php↓
40
+ ```
41
+ <?php
42
+ session_start();
43
+
44
+
45
+ if(isset($_SESSION['user_unique_id'])){
46
+ require_once(ROOT_PATH .'Controllers/UserController.php');
47
+ $user = new UserController();
48
+ $user->InsertMsgImg();
49
+
50
+ }
51
+
52
+
53
+
54
+ ?>
55
+ ```
56
+ insert-img-chat.phpにおける$user->InsertMsgImg()の処理部分が以下です。
57
+
58
+ ```controller
59
+ public function InsertMsgImg(){
60
+ $users = $this->PrivateChatRoom->insert_msg_img();
61
+ return $users;
62
+ }
63
+ ```
64
+ ```ここに言語を入力
65
+ public function insert_msg_img(){
66
+ if(isset($_POST['outgoing_msg_id'])){
67
+ $outgoing_msg_id = $_POST['outgoing_msg_id'];
68
+ }
69
+ if(isset($_POST['incoming_msg_id'])){
70
+ $incoming_msg_id = $_POST['incoming_msg_id'];
71
+ }
72
+ //画像送信
73
+ if($_FILES['msg_img']){
74
+ $msg_img_name = $_FILES['msg_img']['name'];
75
+ $msg_img_type = $_FILES['msg_img']['type'];
76
+ $msg_img_tmp_name = $_FILES['msg_img']['tmp_name'];
77
+
78
+ $msg_img_explode = explode('.',$msg_img_name);
79
+ $msg_img_txt = end($msg_img_explode);
80
+ $extensions = ["png","jpag","jpg"];
81
+
82
+ if(in_array($msg_img_txt,$extensions) === true){
83
+ $time = time();
84
+ $new_img_name = $time.$msg_img_name;
85
+ if(move_uploaded_file($msg_img_tmp_name,"img/".$new_img_name)){
86
+ $msg_img = $new_img_name;
87
+
88
+ }
89
+ }
90
+ }
91
+
92
+
93
+ if(!empty($msg_img)){
94
+ $sql = "INSERT INTO $this->table(outgoing_msg_id,incoming_msg_id,msg_img)
95
+ VALUES(:outgoing_msg_id,:incoming_msg_id,:msg_img)";
96
+ $sth = $this->dbh->prepare($sql);
97
+ }
98
+
99
+
100
+ $this->dbh->beginTransaction();
101
+ try {
102
+ $sth = $this->dbh->prepare($sql);
103
+ $sth->bindParam(':outgoing_msg_id', $outgoing_msg_id, PDO::PARAM_INT);
104
+ $sth->bindParam(':incoming_msg_id', $incoming_msg_id, PDO::PARAM_INT);
105
+ $sth->bindParam(':msg_img', $msg_img, PDO::PARAM_STR);
106
+
107
+ $result = $sth->execute();
108
+ $this->dbh->commit();
109
+ return $result;
110
+ } catch (\PDOException $e) {
111
+ echo '更新失敗'.$e->getMessage();
112
+ $this->dbh->rollBack();
113
+ exit();
114
+ }
115
+ }
116
+ ```
39
117
  insert-img-chat.phpに遷移はデバックでできています。
40
118
  insert-img-chat.php内でのSQL実行に際に$_filesの値が取得できません。
41
119