How to get create success in PHP
How to get create success in PHP
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
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
This discussion has been closed.
Replies
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]
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
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
Allan