beginTransaction(); try { // SQL作成 $stmt = $pdo->prepare("INSERT INTO ueuse (username, account, uniqid, rpuniqid, ueuse, photo1, photo2, video1, datetime, abi) VALUES (:username, :account, :uniqid, :rpuniqid, :ueuse, :photo1, :photo2, :video1, :datetime, :abi)"); $stmt->bindParam(':username', $username, PDO::PARAM_STR); $stmt->bindParam(':account', $userid, PDO::PARAM_STR); $stmt->bindParam(':uniqid', $uniqid, PDO::PARAM_STR); $stmt->bindParam(':rpuniqid', $rpuniqid, PDO::PARAM_STR); $stmt->bindParam(':ueuse', $Rptext, PDO::PARAM_STR); $stmt->bindParam(':photo1', $photo1, PDO::PARAM_STR); $stmt->bindParam(':photo2', $photo2, PDO::PARAM_STR); $stmt->bindParam(':video1', $video1, PDO::PARAM_STR); $stmt->bindParam(':datetime', $datetime, PDO::PARAM_STR); $stmt->bindParam(':abi', $abi, PDO::PARAM_STR); // SQLクエリの実行 $res = $stmt->execute(); // コミット $res = $pdo->commit(); // 書き込み日時を取得 $datetime = date("Y-m-d H:i:s"); $title = ''.$userid.'さんが返信しました!' $msg = ''.$userid.'さんがあなたの投稿に返信しました!' $url = '/!'.$rpuniqid // トランザクション開始 $pdo->beginTransaction(); $stmt = $pdo->prepare("INSERT INTO notification (touserid, title, msg, url, datetime) VALUES (:touserid, :title, :msg, :url, :datetime,)"); $stmt->bindParam(':touserid', $touserid, PDO::PARAM_STR); $stmt->bindParam(':title', $title, PDO::PARAM_STR); $stmt->bindParam(':msg', $msg, PDO::PARAM_STR); $stmt->bindParam(':url', $url, PDO::PARAM_STR); $stmt->bindParam(':datetime', $datetime, PDO::PARAM_STR); $res = $stmt->execute(); $res = $pdo->commit(); } catch(Exception $e) { // エラーが発生した時はロールバック $pdo->rollBack(); } if ($res) { header('Content-Type: application/json'); echo json_encode(['success' => true]); exit; } else { header('Content-Type: application/json'); echo json_encode(['success' => false, 'error' => '追加に失敗しました。']); exit; } } catch(PDOException $e) { header('Content-Type: application/json'); echo json_encode(['success' => false, 'error' => 'データベースエラー:' . $e->getMessage()]); exit; } if (!empty($errors)) { header('Content-Type: application/json'); echo json_encode(['success' => false, 'errors' => $errors]); exit; } // プリペアドステートメントを削除 $stmt = null; } ?>