如何将我的ajax调用输出添加/组合到mysql插入查询?

时间:2015-09-25 10:01:13

标签: javascript mysql json ajax database

我正在尝试使用ajax调用从一个数据库中检索数据,然后将数据放入插入查询中,以便将其添加到新数据库中。 (php mysql to phonegap local db)。这是我需要结合的两个代码。 ajax代码目前只是输出到一个表,我这样做只是为了看它工作。 javascript函数可以正常工作并添加到数据库中。

jQuery(document).ready(function(){


jQuery.ajax({
    url : "http://cmlsys/toby/fetchdata.php",
    type : "POST",
    dataType : "json",
    data : "param=no",
    success : function (html){


        jQuery.each(html, function(key, value){

        $("table#DOM").append('<tr><td>'+value.CurrencyCode+'</td></tr>');

        });


    }, error : function (e){

        alert(e);

    }


});

});

function getEmployees(tx) {
var sql = "select id, CurrencyCode " + "from employee";
tx.executeSql(sql, [], getEmployees_success);

}

function populateDB(tx) {
$('#busy').show();
tx.executeSql('DROP TABLE IF EXISTS employee');
var sql = 
    "CREATE TABLE IF NOT EXISTS employee ( "+
    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
    "CurrencyCode VARCHAR(50))";

tx.executeSql(sql);


tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES (1,'**THE AJAX RETURN**')");

}

以下是我的所有代码 -

var db;
var dbCreated = false;

var scroll = new iScroll('wrapper', { vScrollbar: false, hScrollbar:false, hScroll: false });

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    db = window.openDatabase("EmployeeDirectoryDB", "1.0", "PhoneGap Demo", 200000);
    if (dbCreated)
    	db.transaction(getEmployees, transaction_error);
    else
    	db.transaction(populateDB, transaction_error, populateDB_success);
}

function transaction_error(tx, error) {
	$('#busy').hide();
    alert("Database Error: " + error);
}

function populateDB_success() {
	dbCreated = true;
    db.transaction(getEmployees, transaction_error);
}


function getEmployees(tx) {
	var sql = "select id, CurrencyCode " + "from employee";
	tx.executeSql(sql, [], getEmployees_success);
}


function getEmployees_success(tx, results) {
	
	$('#busy').hide();
    var len = results.rows.length;
    for (var i=0; i<len; i++) {
    	var employee = results.rows.item(i);

		$('#employeeList').append('<p class="line2">' + employee.CurrencyCode + '</p>');
    }
	
	setTimeout(function(){
		scroll.refresh();
	},100);
	db = null;
}



jQuery(document).ready(function () {
        jQuery.ajax({
            url: "http://cmlsys/toby/fetchdata.php",
            type: "POST",
            dataType: "json",
            data: "param=no",
            success: function (html) {                    
                populateDB(tx, html);
                getEmployees(tx);

            },
            error: function (e) {
                alert(e);
            }
        });
    });

    function populateDB(tx, html) {
        $('#busy').show();
        tx.executeSql('DROP TABLE IF EXISTS employee');
        var sql =
            "CREATE TABLE IF NOT EXISTS employee ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "CurrencyCode VARCHAR(50))";

        tx.executeSql(sql);
        var values = jQuery.map(html, function (val, i) {
            return "('" + val.CurrencyCode + "')";
        }).join(',');

        tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES " + values);
    }

	
	

1 个答案:

答案 0 :(得分:1)

您可以在成功结束时将ajax成功数据传递给populateDB函数。请检查以下代码。我猜你的 tx 是一个全局变量。

    var db;
var dbCreated = false;

var scroll = new iScroll('wrapper', {
    vScrollbar: false,
    hScrollbar: false,
    hScroll: false
});

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    db = window.openDatabase("EmployeeDirectoryDB", "1.0", "PhoneGap Demo", 200000);
    if (dbCreated) db.transaction(getEmployees, transaction_error);
    else CallAjax();
}

function transaction_error(tx, error) {
    $('#busy').hide();
    alert("Database Error: " + error);
}

function populateDB_success() {
    dbCreated = true;
    db.transaction(getEmployees, transaction_error);
}


function getEmployees(tx) {
    var sql = "select id, CurrencyCode from employee";
    tx.executeSql(sql, [], getEmployees_success);
}


function getEmployees_success(tx, results) {

    $('#busy').hide();
    var len = results.rows.length;
    for (var i = 0; i < len; i++) {
        var employee = results.rows.item(i);

        $('#employeeList').append('<p class="line2">' + employee.CurrencyCode + '</p>');
    }

    setTimeout(function () {
        scroll.refresh();
    }, 100);
    db = null;
}


function CallAjax() {
    jQuery.ajax({
        url: "http://cmlsys/toby/fetchdata.php",
        type: "POST",
        dataType: "json",
        data: "param=no",
        success: function (html) {
            $('#busy').show();
            db.transaction(function (tx) {
                tx.executeSql('DROP TABLE IF EXISTS employee');
                var sql =
                    "CREATE TABLE IF NOT EXISTS employee ( " +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "CurrencyCode VARCHAR(50))";

                tx.executeSql(sql);
                var values = jQuery.map(html, function (val, i) {
                    return "('" + val.CurrencyCode + "')";
                }).join(',');

                tx.executeSql("INSERT INTO employee (id,CurrencyCode) VALUES " + values);
            }, transaction_error, populateDB_success);

        },
        error: function (e) {
            alert(e);
        }
    });
}