\PDO::FETCH_ASSOC, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'" ]); // $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE , PDO::FETCH_ASSOC ); $terminals = array(); $ts = time(); $result = array('res' => 0, 'data' => array()); $lineId = isset($_POST['lineId']) ? intval($_POST['lineId']) : 0; $cmdids = isset($_POST['cmdids']) ? $_POST['cmdids'] : ''; $channels = isset($_POST['channels']) ? intval($_POST['channels']) : 1; $protocol = isset($_POST['protocol']) ? intval($_POST['protocol']) : 0; $contents = str_replace("\r\n", "\n", $cmdids); $contents = str_replace("\n\r", "\n", $contents); $contents = str_replace("\r", "\n", $contents); $cmdids = explode("\n", $contents); $stmt = $db->prepare("SELECT * FROM `lines` WHERE `id`=" . $lineId); $stmt->execute(); $rows = $stmt->fetchAll(); $stmt = null; $lineName = ''; if ($rows) { $lineName = $rows[0]['name']; } $ids = array(); $rows = array(); foreach ($cmdids as $cmdid) { $cmdid = trim($cmdid); if (empty($cmdid)) continue; $db->query("INSERT INTO `towers`(`line_id`, `name`) VALUES($lineId, '" . $cmdid . "')"); $tower_id = $db->lastInsertId(); $db->query("INSERT INTO `terminals`(`line_id`, `tower_id`, `cmdid`, `protocol`) VALUES($lineId, " . $tower_id . ",'" . $cmdid . "'," . $protocol . ")"); $term_id = $db->lastInsertId(); for ($channel = 1; $channel <= $channels; $channel++) { $db->query("INSERT INTO `terminal_channel_mapper`(`term_id`, `channel_id`) VALUES(" . $term_id . "," . $channel . ")"); } $ids[] = $term_id; $row['id'] = $term_id; $row['cmdid'] = $cmdid; $row['protocol'] = $protocol; $row['line_name'] = $lineName; $row['tower_name'] = $cmdid; $row['display_name'] = ''; $rows[] = $row; unset($row); } $result['data'] = $rows; header ('Content-type: application/json; charset=utf-8'); echo json_encode($result, JSON_UNESCAPED_UNICODE);