jQWidgets Forums

jQuery UI Widgets Forums Grid Addrow… how to get the ID of the newly created record ?

This topic contains 1 reply, has 1 voice, and was last updated by  tuthmosis 12 years ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author

  • tuthmosis
    Member

    Greetings !

    Another super obvious need…

    I will create new rows but the final primary key will be selected by MySQL.

    I cannot create one on client side since this application will be used by more than one users simultaneously… there will be collisions for sure.

    In PHP i would use “mysql_insert_id()” which returns the ID of the last record created for the current db connection.

    How to do this with jqxgrid ?

    Thanks !


    tuthmosis
    Member

    I guess the following would be the answer…

    In data.php… Changing :

    	$insert_query = "INSERT INTO `sfd__sometable`(`somefieldname`) VALUES ('".$_GET['somevalue']."')";
    $result = mysql_query($insert_query) or die("SQL Error 1: " . mysql_error());
    echo $result; //To get removed !

    to :

    	$insert_query = "INSERT INTO `sfd__sometable`(`somefieldname`) VALUES ('".$_GET['somevalue']."')";
    $result = mysql_query($insert_query) or die("SQL Error 1: " . mysql_error());
    ==>> echo json_encode(mysql_insert_id());

    And in the javascript… changing the commit call in success:

                    addrow: function (rowid, rowdata, position, commit) {
    // synchronize with the server - send insert command
    rowdata.id = '';
    var data = "insert=true&" + $.param(rowdata);
    $.ajax({
    dataType: 'json',
    url: 'data.php',
    data: data,
    success: function (data, status, xhr) {
    // insert command is executed.
    commit(true);
    },
    error: function (jqXHR, textStatus, errorThrown) {
    commit(false);
    }
    });
    }

    for
    addrow: function (rowid, rowdata, position, commit) {

                        // synchronize with the server - send insert command
    rowdata.id = '';
    var data = "insert=true&" + $.param(rowdata);
    $.ajax({
    dataType: 'json',
    url: 'data.php',
    data: data,
    success: function (data, status, xhr) {
    // insert command is executed.
    commit(true, data);
    },
    error: function (jqXHR, textStatus, errorThrown) {
    commit(false);
    }
    });
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.