Insert data in other table
Insert data in other table
andreavellone
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
This discussion has been closed.
Answers
There is no
$row
variable in yourinsertLinkTable
. It looks like it should be$values['materiale_id']
.Allan
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;
$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')
<?php > ``` ?>->leftJoin('fornitori', 'fornitori.id', '=' , 'movimenti.fornitore_id')
->leftJoin('ol', 'ol.id', '=' , 'movimenti.ad_ol_id')
->leftJoin('utenti', 'utenti.id', '=' , 'movimenti.user_id')
but don't run
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
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
and this is the table vardisp
Thanks - I see the issue. Because you have a table name before the column name you need to access it like this:
Allan
It's a good monday. It run. Thanks a lot..
mmmm somenthing strange.
The insert it's ok but non the same for the edit with the inline edit...
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')
<?php > ``` ?>->leftJoin('fornitori', 'fornitori.id', '=' , 'movimenti.fornitore_id')
->leftJoin('ol', 'ol.id', '=' , 'movimenti.ad_ol_id')
->leftJoin('utenti', 'utenti.id', '=' , 'movimenti.user_id')
No... sorry i made a stupid error i used only postCreate
Thanks for posting back. Good to hear that it is working now.
Allan
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']
My guess is that you are using inline editing and not using the
allIfChanged
option of thesubmit
option inform-options
?Allan
yes. I changed. Now run.
thank a lot
A.