Is a Table entry "PHP only" possible?

Is a Table entry "PHP only" possible?

Oliver RitterOliver Ritter Posts: 2Questions: 1Answers: 0

Hi,
I work with a lot of enthusiasm with DataTables Editor .
Is it possible to make a table entry directly in the PHP file?
My idea looks like Follows:

include( "DataTables.php"   );

use DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;


$_POST['action']= 'create';         
$_POST['data']['DT_RowId']= 'row_1';
$_POST['data']['id']= '2';
$_POST['data']['username']='Mustermann';
$_POST['data']['useremail']='Mustermann';
$_POST['data']['userpassword']='Mustermann';

$editor =   Editor::inst(   $db, 'quizuser' )
        ->fields(
                Field::inst( 'id'),
                Field::inst( 'username' ),
                Field::inst( 'useremail' ),
                Field::inst( 'userpassword' )
                    )
            ->process($_POST)
            ->json();

But the result unfortunately looks like:

Fatal error: Call to a member function insertId() on null in E:\xampp_php_5.6.30\htdocs\quiz\php\Editor\Editor.php on line 1624

Have I overlooked something important?
Thanks for your support.

Answers

  • allanallan Posts: 63,482Questions: 1Answers: 10,467 Site admin

    You are missing a dimension in your array:

    $input = array(
      "action" => "create",
      "data" => array()
    );
    
    $input['data'][0] = array();
    $input['data'][0]['username']='Mustermann';
    $input['data'][0]['useremail']='Mustermann';
    $input['data'][0]['userpassword']='Mustermann';
     
    $editor =   Editor::inst(   $db, 'quizuser' )
            ->fields(
                    Field::inst( 'id'),
                    Field::inst( 'username' ),
                    Field::inst( 'useremail' ),
                    Field::inst( 'userpassword' )
                        )
                ->process($input)
                ->json();
    

    I've used a plain array rather than manipulating the $_POST global which is bad practice.

    The key is the extra [0] in the data array for the create action. Editor can submit multiple rows to be created at the same time which is why that is required.

    You also had an id in the faked data. An id would exist until the row has been created.

    Regards,
    Allan

  • Oliver RitterOliver Ritter Posts: 2Questions: 1Answers: 0

    Hi Allan,

    thanks for your quick and very detailed response. I will try it out in practice.

This discussion has been closed.