From 81860d6800f25de8f5fb29df0d76caf62b968a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=A2=E1=84=91=E1=85=B3=E1=86=AF=20=E1=84=82?= =?UTF-8?q?=E1=85=A9=E1=84=90=E1=85=B3=E1=84=87=E1=85=AE=E1=86=A8?= Date: Wed, 4 Jul 2018 20:09:17 +0900 Subject: [PATCH] Add: edit player --- src/web/js/player_list_manager.js | 53 ++++++++++++++++++- src/web/server/user/edit_player.php | 80 +++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 src/web/server/user/edit_player.php diff --git a/src/web/js/player_list_manager.js b/src/web/js/player_list_manager.js index 68d7e48..fd5860c 100644 --- a/src/web/js/player_list_manager.js +++ b/src/web/js/player_list_manager.js @@ -290,12 +290,61 @@ editPlayer(inputButton) { // console.log(inputButton); + let clickedObjectID = this.getClickedObjectID(inputButton); + let id = $(inputButton).siblings(".player_id"); - // console.log(id); let playerID = id.text(); + // console.log(playerID); - console.log(playerID); + let name = $(inputButton).siblings(".player_name"); + let playerName = name.val(); + // console.log(playerName); + + let enterCode = $(inputButton).siblings(".player_entercode"); + let playerEnterCode = enterCode.val(); + // console.log(playerEnterCode); + + let xhr = new XMLHttpRequest(); //new로 생성. + xhr.open('POST', './../server/user/edit_player.php', true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.send("maestro_id=" + maestroID + "&player_id=" + playerID + "&player_name=" + playerName + "&enter_code=" + playerEnterCode); + xhr.onload = function() { + if(xhr.readyState === 4 && xhr.status === 200) { + // console.log(xhr.responseText); + // console.log(xhr.responseText.length); + if(xhr.responseText.length === 0 || xhr.responseText === null) { + console.log("no data from server"); + return; + } + + let replyJSON = JSON.parse(xhr.responseText); + // console.log(replyJSON); + + if(replyJSON === null) { + console.log("no data from server"); + return; + } + + if(replyJSON["ERROR"] !== undefined || replyJSON["RESULT"] === undefined) { + console.log(replyJSON["ERROR"]); + return; + } + + if(replyJSON["RESULT"] === "fail") { + console.log(replyJSON["RESULT"]); + return; + } + + console.log(clickedObjectID); + if(clickedObjectID === "add_player_list") { + self.setupAddPlayerList(); + } else if(clickedObjectID === "search_player_list") { + let playerName = $("#search_name").val(); + self.setupSearchPlayerList(playerName); + } + } + } } deletePlayer(inputButton) { diff --git a/src/web/server/user/edit_player.php b/src/web/server/user/edit_player.php new file mode 100644 index 0000000..3768b3a --- /dev/null +++ b/src/web/server/user/edit_player.php @@ -0,0 +1,80 @@ +close(); + exit; +} + +editPlayer($maestroID, $playerID, $playerName, $enterCode); + +$result = hasPlayerName($maestroID, $playerName, $enterCode); +if($result === null) { + send_result_message($replyJSON, "fail"); + exit; +} + +$replyJSON["RESULT"] = "success"; +echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); +$db_conn->close(); + +function hasSamePlayerData($maestroID, $playerID, $playerName, $enterCode) { + global $db_conn; + + $query = "SELECT UserID FROM moty_user WHERE MaestroID=? AND UserID<>? AND Name=? AND EnterCode=?"; + $stmt = $db_conn->prepare($query); + $stmt->bind_param('iiss', $maestroID, $playerID, $playerName, $enterCode); + $stmt->execute(); + $stmt->bind_result($userID); + // while($stmt->fetch()) + $stmt->fetch(); + $stmt->close(); + + // $replyJSON["UserID"] = $userID; + return $userID; +} + +function editPlayer($maestroID, $playerID, $playerName, $enterCode) { + global $db_conn; + + $query = "UPDATE moty_user SET Name=?, EnterCode=? WHERE MaestroID=? AND UserID=?"; + $stmt = $db_conn->prepare($query); + $stmt->bind_param('ssii', $playerName, $enterCode, $maestroID, $playerID); + $stmt->execute(); +} + +function hasPlayerName($maestroID, $playerName, $enterCode) { + global $db_conn; + + $query = "SELECT UserID FROM moty_user WHERE MaestroID=? AND Name=? AND EnterCode=?"; + $stmt = $db_conn->prepare($query); + $stmt->bind_param('iss', $maestroID, $playerName, $enterCode); + $stmt->execute(); + $stmt->bind_result($userID); + // while($stmt->fetch()) + $stmt->fetch(); + $stmt->close(); + + // $replyJSON["UserID"] = $userID; + return $userID; +} + +?> \ No newline at end of file