'Why is PHP File Adding SQL Records Twice

I have the following code, where the user inputs information to a table, then when clicking an "upload" button, the following JS function is called, where an AJAX request is sent to the PHP file to insert the user entered data into a SQLite3 database. For some reason, everytime I submit, the SQLite DB has 2 duplicate records for each row of the input table. It seems like either the JS function or PHP file is adding each table row twice, although I'm not able to pinpoint where it is occurring. Any advice would be greatly appreciated.

        function uploadTable(){
            var data = document.getElementsByClassName("manUploadRow");
            resp = "";
            for(i=0; i<data.length; i++){
                xmlhttp = new XMLHttpRequest();
                var term=data[i].children[0].children[0].value;
                var cid=data[i].children[1].children[0].value;
                var cname=data[i].children[2].children[0].value;
                var prof=data[i].children[3].children[0].value;
                xmlhttp.open("GET", "manUpload.php?term="+term+"&cCode="+cid+"&cName="+cname+"&prof="+prof, false);
                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        resp = resp + this.responseText;
                        document.getElementById("txtHint").innerHTML = resp;
                    }
                }
                xmlhttp.send();
            }
        }
<?php
require_once "functions.php";
$term = $_GET["term"];
$cCode =  $_GET["cCode"];
$cName =  $_GET["cName"];
$prof =  $_GET["prof"];

$db = new SQLite3("../database/database.db");
$query = "insert into course (coursecode, coursename, prof, term) values ( '$cCode', '$cName', '$prof', '$term');";
echo $query;
if($db->exec($query)){
    echo "Successfully Registered";
}
else {
    echo "Failed to enter into database, see the following error code: ";
    echo $db->lastErrorMsg();
}
$db->close();
unset($db);
?>
                            <tr id="templateRow-1" class="manUploadRow">
                                <td><input type="text" id="row-1-term" name="row-1-term" placeholder="Term with Month and Year"></td>
                                <td><input type="number" id="row-1-num" name="row-1-num" placeholder="Course Number"></td>
                                <td><input type="text" id="row-1-name" name="row-1-name" placeholder="Course Name"></td>
                                <td><input type="text" id="row-1-instructor" name="row-1-instructor" placeholder="Instructor"></td>
                            </tr>
                    <tr id="templateRow-2" class="manUploadRow">
                                <td><input type="text" id="row-2-term" name="row-2-term" placeholder="Term with Month and Year"></td>
                                <td><input type="number" id="row-2-num" name="row-2-num" placeholder="Course Number"></td>
                                <td><input type="text" id="row-2-name" name="row-2-name" placeholder="Course Name"></td>
                                <td><input type="text" id="row-2-instructor" name="row-2-instructor" placeholder="Instructor"></td>
                            </tr></tbody>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source