How to get create success in PHP

How to get create success in PHP

AlfredAlfred Posts: 11Questions: 0Answers: 0
edited January 2014 in Editor
Using Editor I would like to be informed if a create was successful. Best with the last insert id. Can i get the information dirct in PHP or must i go via Javascript and a AJAX Request as a workaround?

basic constuct is:

[code]
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;

$editor= Editor::inst( $db, 'users','id' )
->fields(
Field::inst( Name') ->validator( 'Validate::required' ),
Field::inst( 'rubbish' )
);

$editor ->process( $_POST );

if (isset($_POST['action']) && $_POST['action'] == 'create' ){

// if create was successful then do some stuff
// how to detect if there was no error and the id of the record

}
$editor ->json();

[/code]

Any help is appreciated.

Thanks

Replies

  • Matthew2051Matthew2051 Posts: 13Questions: 1Answers: 0
    I'd be interested in knowing if this is possible too. I'd use it slightly differently where the action is 'edit', using the info stored in $_POST['data'] I would write into another table along with a user ID to keep track of who is editing what for historic records. Knowing whether the 'Editor' has successfully written to the DB would be handy, specially if it was all done in php.
  • AlfredAlfred Posts: 11Questions: 0Answers: 0
    edited January 2014
    Well,

    I have made a workaround with native stuff. May be not the most elegant way but it works.

    [code]
    global $db_connect;
    $ret=mysqli_fetch_array(mysqli_query($db_connect,"SELECT count(*) FROM ".user_table));
    $count = $ret[0];

    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Validate;


    $editor= Editor::inst( foo.....stuff

    $editor
    ->process( $_POST );

    if (isset($_POST['action']) && $_POST['action'] == 'create' ){
    global $db_connect;
    $ret=mysqli_fetch_array(mysqli_query($db_connect,"SELECT count(*), MAX(ID) FROM ".user_table));

    if ($count != $ret[0]) {
    $last_insert=$ret[1];
    .... do other stuff
    }
    }

    $editor
    ->json();

    [/code]
  • allanallan Posts: 63,531Questions: 1Answers: 10,474 Site admin
    You can use the `data()` method ( https://editor.datatables.net/docs/current/php/class-DataTables.Editor.html#_data ) to get the data that the Editor class has processed. It is basically the same as `json()` but returns the data in an array format, rather than outputting as JSON as `json()` does.

    From there you can look at the data to check if there are any errors. The data format is described here: https://editor.datatables.net/server/

    Allan
  • AlfredAlfred Posts: 11Questions: 0Answers: 0
    edited January 2014
    Hi allan,

    thanks. Well $editor->data() returns (nearly) the same $_POST does incl. the 'fielderrors' .

    It would be nice if there can be some sql Information like: mysql_error, mysql_errno and mysql_insert_id in the data() to.

    Or did you mean:

    ["id"]=>
    string(5) "row_5"
    ["fieldErrors"]=>
    array(0) {
    }
    ["sError"]=>
    string(0) ""
    ["aaData"]=>
    array(0) {
    }

    no error means success and 'row_x' is the insert_id ?

    Alfred
  • allanallan Posts: 63,531Questions: 1Answers: 10,474 Site admin
    That's it - bob on :-)

    Allan
  • AlfredAlfred Posts: 11Questions: 0Answers: 0
    great :-) close it
This discussion has been closed.