Insert data in other table

Insert data in other table

andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

Hi,
I have this function

function insertLinkTable ( $db, $action, $id, $values ) {
    $db->insert( 'vardisp', array(
        'movid' => $id ,
        'matid' => $row['materiale_id']
    ) );
}

I want to save in a different table (a link table)

I call the function in this way

->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        insertLinkTable( $editor->db(), 'create', $id, $values );}

The result is that i insert $id but i can't fine the way to insert materiale_id because is indefined.
I try in other way $materiale_id, $_POST['materiale_id'] and so on.
But don't run.

Any idea?
Tx a lot

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    There is no $row variable in your insertLinkTable. It looks like it should be $values['materiale_id'].

    Allan

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    I tried of course
    ```
    <?php

    /*
    * Example PHP implementation used for the index.html example
    */

    // DataTables PHP library
    include( "../../php/DataTables.php" );

    function insertLinkTable ( $db, $action, $id, $values ) {
    $db->insert( 'vardisp', array(
    'movid' => $id ,
    'matid' => $values['materiale_id']
    ) );
    }

    //echo $movid;

    //if ( isset( $_POST['action'] ) && $_POST['action'] === 'edit' ) {
    //$db->insert( 'vardisp', [ 'movid' => $_POST['data']['id'] ] );
    //}

    // Alias Editor classes so they are easy to use
    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

    // Allow a number of different formats to be submitted for the various demos
    

    $format = isset( $_GET['format'] ) ?
    $_GET['format'] :
    '';

    if ( $format === 'custom' ) {
    $update = 'n/j/Y';
    $registered = 'l j F Y';
    }
    else {
    $update = Format::DATE_ISO_8601;
    $registered = Format::DATE_ISO_8601;
    }

    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'movimenti' )
    ->fields(
    Field::inst( 'movimenti.user_id' )
    ->options( Options::inst()
    ->table( 'utenti' )
    ->value( 'id' )
    ->label( 'nome' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'utenti.nome' ),
    Field::inst( 'movimenti.ad_ol_id' )
    ->options( Options::inst()
    ->table( 'ol' )
    ->value( 'id' )
    ->label( 'id' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'ol.id' ),
    Field::inst( 'movimenti.materiale_id' )
    ->options( Options::inst()
    ->table( 'materiali' )
    ->value( 'id' )
    ->label( 'codice' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'materiali.codice' ),
    Field::inst( 'movimenti.quantita' ),
    Field::inst( 'movimenti.fatturazione_italtel' ),
    Field::inst( 'movimenti.fornitore_id' )
    ->options( Options::inst()
    ->table( 'fornitori' )
    ->value( 'id' )
    ->label( 'nome' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'fornitori.nome' ),
    Field::inst( 'movimenti.var' ),
    Field::inst( 'materiali.disponibilita' )

    )
    

    ->leftJoin('materiali', 'materiali.id', '=' , 'movimenti.materiale_id')
    ->leftJoin('fornitori', 'fornitori.id', '=' , 'movimenti.fornitore_id')
    ->leftJoin('ol', 'ol.id', '=' , 'movimenti.ad_ol_id')
    ->leftJoin('utenti', 'utenti.id', '=' , 'movimenti.user_id')

    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        insertLinkTable( $editor->db(), 'create', $id, $values );
    } ) 
    
    ->process( $_POST )
    ->json();
    
    <?php > ``` ?>
  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    but don't run

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I don't immediately see the issue there. If you take a look in your server's error log, does it show any messages?

    Thanks,
    Allan

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    yes, i have an indefined index

    [Fri Nov 17 10:46:16.228634 2017] [:error] [pid 823:tid 139935453468416] [client 79.8.3.172:56225] PHP Notice: Undefined index: materiale_id in /home/temisges/public_html/tg/files/php/db-temis-movimenti.php on line 39, referer: http://temisgest.it/tg/files/inline/temis-movimenti.php?title=%3Cb%3EMateriali%3C/b%3E-%3E%20Disponibilit%C3%A0

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    and this is the table vardisp

    -- Struttura della tabella `vardisp`
    --
    
    CREATE TABLE `vardisp` (
      `id` int(11) NOT NULL,
      `movid` int(11) NOT NULL,
      `matid` int(11) NOT NULL,
      `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    --
    -- Indici per le tabelle scaricate
    --
    
    --
    -- Indici per le tabelle `vardisp`
    --
    ALTER TABLE `vardisp`
      ADD PRIMARY KEY (`id`);
    
    --
    -- AUTO_INCREMENT per le tabelle scaricate
    --
    
    --
    -- AUTO_INCREMENT per la tabella `vardisp`
    --
    ALTER TABLE `vardisp`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Thanks - I see the issue. Because you have a table name before the column name you need to access it like this:

    $values[''movimenti']['materiale_id']
    

    Allan

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    It's a good monday. It run. Thanks a lot..

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    mmmm somenthing strange.

    The insert it's ok but non the same for the edit with the inline edit...

    var editor; // use a global for the submit and return data rendering in the examples
    
    $(document).ready(function() {
        editor = new $.fn.dataTable.Editor( {
            ajax: "../php/db-temis-movimenti.php",
            table: "#example",
    
            fields: [ {
                    label: "Utente",
                    name: "movimenti.user_id",
                    type: "select"
                    
                }, {
                    label: "Per OL:",
                    name: "movimenti.ad_ol_id",
                    type: "select"
                }, {
                    label: "Materiali",
                    name: "movimenti.materiale_id",
                    type: "select"
                },{
                    label: "Quantità",
                    name: "movimenti.quantita"
                },{
                    label:     "Fatturazione Italtel:",
                    name:      "movimenti.fatturazione_italtel",
                    type:      "select",
                    separator: "|",
                    options:   [
                       { label: "NO", value: 0 },
                                { label: "SI",    value: 1 }
                    ]
                }, {
                    label: "Fornitore",
                    name: "movimenti.fornitore_id",
                    type: "select"
                }, {
                    label: "Dal magazzino",
                    name: "movimenti.var"
                }
                
            ]
        } );
    
        // Activate an inline edit on click of a table cell
         
    
    $('#example').on( 'click', 'tbody td:not(:first-child)', function (e) {
            editor.inline( this, {
                submit: 'all'
            } );
        } );
    
    
        $('#example').DataTable( {
            dom: "Bfrtip",
            ajax: "../php/db-temis-movimenti.php",
            order: [[0, 'asc']],
    
            columns: [
                {
                    data: null,
                    defaultContent: '',
                    className: 'select-checkbox',
                    orderable: false
                },
            
                { data: "utenti.nome",  editField: "movimenti.user_id" },
                { data: "ol.id", editField: "movimenti.ad_ol_id"},
                { data: "materiali.codice", editField: "movimenti.materiale_id" },
                { data: "movimenti.quantita" },
                {
                    data:   "movimenti.fatturazione_italtel",
                    type:  "select",
                    "render": function (val, type, row) {
                        return val == 0 ? "NO" : "SI"; }
                 }, 
                 { data: "fornitori.nome", editField: "movimenti.fornitore_id" },
                { data: "movimenti.var"}
                
                
            ],
            select: {
                style:    'os',
                selector: 'td:first-child'
            },
            buttons: [
            
            {
                        extend: "create",
                        text:"+",
                        editor: editor,
                        className: 'btn btn-success waves-effect waves-light m-r-5',
                        action: function (e, dt, node, config) {
     
                            //var id = $("#id").val();
                           // var DebtorId = $('#SearchDebtorInvoice').data('debtorId');
     
                            editor
                                    .create(false)
                                    .set('movimenti.user_id')
                                    .set('movimenti.ad_ol_id')
                                    .set('movimenti.materiale_id')
                                    .set('movimenti.quantita')
                                    .set('movimenti.fatturazione_italtel')
                                    .set('movimenti.fornitore_id')
                                    .set('movimenti.var')
                                    .submit();
     
                        }  
                    },
            
            
            
            
                { extend: 'create', editor: editor },
                { extend: 'edit',   editor: editor },
                { extend: 'remove', editor: editor },
                {
                    extend: 'collection',
                    text: 'Export',
                    buttons: [
                        "copy",
                        "excel",
                        "csv",
                        "pdf",
                        "print"
                        
                    ]
                }
            ]
            
        } );
        
    } );
    
    
        </script>
    
    

    here he server-side code

    ```
    function insertLinkTable ( $db, $action, $id, $values ) {
    $db->insert( 'vardisp', array(
    'vardsp.movid' => $id ,
    'vardisp.matid' => $values['movimenti']['materiale_id']
    ) );
    }

    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'movimenti' )
    ->fields(
    Field::inst( 'movimenti.user_id' )
    ->options( Options::inst()
    ->table( 'utenti' )
    ->value( 'id' )
    ->label( 'nome' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'utenti.nome' ),
    Field::inst( 'movimenti.ad_ol_id' )
    ->options( Options::inst()
    ->table( 'ol' )
    ->value( 'id' )
    ->label( 'id' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'ol.id' ),
    Field::inst( 'movimenti.materiale_id' )
    ->options( Options::inst()
    ->table( 'materiali' )
    ->value( 'id' )
    ->label( 'codice' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'materiali.codice' ),
    Field::inst( 'movimenti.quantita' ),
    Field::inst( 'movimenti.fatturazione_italtel' ),
    Field::inst( 'movimenti.fornitore_id' )
    ->options( Options::inst()
    ->table( 'fornitori' )
    ->value( 'id' )
    ->label( 'nome' )
    )
    ->validator( 'Validate::dbValues' ),
    Field::inst( 'fornitori.nome' ),
    Field::inst( 'movimenti.var' )
    )

    ->leftJoin('materiali', 'materiali.id', '=' , 'movimenti.materiale_id')
    ->leftJoin('fornitori', 'fornitori.id', '=' , 'movimenti.fornitore_id')
    ->leftJoin('ol', 'ol.id', '=' , 'movimenti.ad_ol_id')
    ->leftJoin('utenti', 'utenti.id', '=' , 'movimenti.user_id')

     ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        insertLinkTable( $editor->db(), 'edit', $id, $values );
    } )
    
     ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        insertLinkTable( $editor->db(), 'create', $id, $values );
    } )      
    
    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        insertLinkTable( $editor->db(), 'delete', $id, $values );
    } ) 
    
    ->process( $_POST )
    ->json();
    
    <?php > ``` ?>
  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    No... sorry i made a stupid error :) i used only postCreate

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Thanks for posting back. Good to hear that it is working now.

    Allan

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    HI allan, in your opinion why with insert it's all ok, on the contrary when i try to update i have an error "Undefined index" for the value $values['movimenti']['materiale_id']

    function insertLinkTable ( $db, $action, $id, $values ) {
        $db->insert( 'vardisp', array(
            'movid' => $id ,
            'matid' => $values['movimenti']['materiale_id']
       ) );
    }
    
    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
            insertLinkTable( $editor->db(), 'edit', $id, $values );
        } )
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited November 2017 Answer ✓

    My guess is that you are using inline editing and not using the allIfChanged option of the submit option in form-options?

    Allan

  • andreavelloneandreavellone Posts: 46Questions: 13Answers: 2

    yes. I changed. Now run.
    thank a lot
    A.

This discussion has been closed.