$result = mysqli_query($link, $query); if (mysqli_num_rows($result) == 0) { // insert into user course $insertQuery = "INSERT INTO user_courses (email,sectionNumber, deptAbbreviation, term) VALUES ('" . userEmail() . "'," . $sectionNumber . ",'" . $deptAbbreviation . "'," . $term . ")"; mysqli_query($link, $insertQuery); if (mysqli_errno($link) != 0) { sqlCommRollChanges(1, $link, $publishedCourseName); } // set first_login to false $updateQuery = "UPDATE users SET first_login=0 WHERE email='" . userEmail() . "'"; mysqli_query($link, $updateQuery); if (mysqli_errno($link) != 0) { sqlCommRollChanges(1, $link, $publishedCourseName); } } sqlCommRollChanges(0, $link, $publishedCourseName); // check the sql_error flag to see if there were any errors in the sql queries // if there were rollback the changes and echo error // else commit changes and echo success function sqlCommRollChanges($sql_error, $sqlLink, $pcn) { $response = ""; if ($sql_error == 0) { mysqli_commit($sqlLink); $response['message'] = "The course " . $pcn . " was successfully added to your watch-list"; } else { mysqli_rollback($sqlLink); $response['message'] = "There was an error processing your request. Please try again later."; } echo json_encode($response); exit(0);
} mysqli_query($link, $updateQuery); if (mysqli_errno($link) != 0) { sqlCommRollChanges(1, mysqli_errno($link), mysqli_error($link), $link); } if ($type == "Kit") { // If Kit, Update Kit Status $updateQuery = "UPDATE kits SET status = 2 WHERE kitid='" . $itemID . "' AND status <>-1"; mysqli_query($link, $updateQuery); if (mysqli_errno($link) != 0) { sqlCommRollChanges(1, mysqli_errno($link), mysqli_error($link), $link); } } } // end forEach loop sqlCommRollChanges(0, 0, "", $link); function sqlCommRollChanges($status, $sql_errno, $sql_error, $sqlLink) { $response = ""; if ($status == 0) { mysqli_commit($sqlLink); $response['status'] = 0; $response['message'] = "All loan items were successfully checked out."; } else { mysqli_rollback($sqlLink); $response['status'] = 1; $response['message'] = $sql_errno . ": " . $sql_error . " No loan items were checked out."; } echo json_encode($response); exit(0); }