From 3616ac1a1d842ca2d4bb620ad033013d5243c1d1 Mon Sep 17 00:00:00 2001 From: IceTeaXXD <13521024@std.stei.itb.ac.id> Date: Wed, 15 Nov 2023 13:32:38 +0700 Subject: [PATCH] fix: university sync --- api/polling/universitysync.php | 62 +++++++--------------------------- 1 file changed, 12 insertions(+), 50 deletions(-) diff --git a/api/polling/universitysync.php b/api/polling/universitysync.php index d5ccb9a..c47c127 100644 --- a/api/polling/universitysync.php +++ b/api/polling/universitysync.php @@ -11,64 +11,26 @@ $response = $soap->doRequest("getAllUniversities", null); $db = new Database(); if ($response->return != null) { - if (is_array($response->return)) { - for ($i = 0; $i < count($response->return); $i++) { - $uni_name = $response->return[$i]->name; - $phpUniId = $response->return[$i]->phpUniId; - $restUniId = $response->return[$i]->restUniId; - - if ($phpUniId == -1) { - $query = "SELECT * FROM university WHERE name = ?"; - $stmt = $db->setSTMT($query); - mysqli_stmt_bind_param($stmt, "s", $uni_name); - $exec = mysqli_stmt_execute($stmt); - /* If there is this instance do a soap request*/ - if ($exec) { - $res = mysqli_stmt_get_result($stmt); - $row = mysqli_fetch_array($res); - $params = array("php_uni_id" => $row['university_id'], "rest_uni_id" => $restUniId); - $response = $soap->doRequest("setPHPId", $params); - } else { - /* Create the instance in PHP*/ - $query = "INSERT INTO university (name) VALUES ?"; - $stmt = $db->setSTMT($query); - mysqli_stmt_bind_param($stmt, "s", $uni_name); - $exec = mysqli_stmt_execute($stmt); - if ($exec) { - $id = mysqli_insert_id($db->getDatabase()); - $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); - $response = $soap->doRequest("setPHPId", $params); - } - } - } - } - } else { - $uni_name = $response->return->name; - $phpUniId = $response->return->phpUniId; - $restUniId = $response->return->restUniId; + if (!is_array($response->return)) { + $response->return = array($response->return); + } + foreach ($response->return as $uni) { + $uni_name = $uni->name; + $phpUniId = $uni->phpUniId; + $restUniId = $uni->restUniId; if ($phpUniId == -1) { - $query = "SELECT * FROM university WHERE name = ?"; + // Insert to database + $query = "INSERT INTO university (name) VALUES (?)"; $stmt = $db->setSTMT($query); mysqli_stmt_bind_param($stmt, "s", $uni_name); $exec = mysqli_stmt_execute($stmt); - /* If there is this instance do a soap request*/ if ($exec) { - $res = mysqli_stmt_get_result($stmt); - $row = mysqli_fetch_array($res); - $params = array("php_uni_id" => $row["university_id"], "rest_uni_id" => $restUniId); + $id = mysqli_insert_id($db->getDatabase()); + $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); $response = $soap->doRequest("setPHPId", $params); } else { - /* Create the instance in PHP*/ - $query = "INSERT INTO university (name) VALUES ?"; - $stmt = $db->setSTMT($query); - mysqli_stmt_bind_param($stmt, "s", $uni_name); - $exec = mysqli_stmt_execute($stmt); - if ($exec) { - $id = mysqli_insert_id($db->getDatabase()); - $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); - $response = $soap->doRequest("setPHPId", $params); - } + echo "Error: " . mysqli_error($db->getDatabase()); } } } -- GitLab