Execute after Update

Execute after Update

cemlimitedcemlimited Posts: 36Questions: 9Answers: 0
edited December 2021 in Editor

I am unable to execute a Query AFTER an update or insert

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


$aaa =Editor::inst( $db, $table );

$aaa->where( 'brief', $brief );
//$aaa->readTable('taskdetail');
foreach(array_diff($fields,$excludes,$selects, $dates, $boolean) as $field){
    $aaa ->field(Field::inst( $table.".".$field));
}

$cycle = 0;

foreach($selects as $select){
    $aaa ->field(Field::inst($table.'.'.$select)->options( Options::inst()->table( $selectstable[$cycle] )->value( $selectssource[$cycle] )->label( $selectslabels[$cycle] ))->validator( Validate::dbValues()));
    $aaa ->field(Field::inst( $selectstable[$cycle].'.'.$selectslabels[$cycle]));
    $aaa ->leftJoin( $selectstable[$cycle],$selectstable[$cycle].'.'.$selectssource[$cycle], '=', $table.'.'.$select );
    $cycle = $cycle + 1;
}

$cycle = 0;


foreach($dates as $date){
    $aaa ->field(Field::inst($table.'.'.$date)->validator( Validate::dateFormat('Y-m-d'))->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )->setFormatter( Format::dateFormatToSql('Y-m-d' ) ));
}

foreach($boolean as $boo){
    $aaa ->field(Field::inst($table.'.'.$boo)->setFormatter( function ( $val, $data, $opts ) {return ! $val ? 0 : 1;} ));
}



$aaa->process( $_POST )->json();


if ($_POST['action'] == 'create' ) {
    $fieldquery = "SELECT * FROM `tasks` ORDER BY `id` DESC LIMIT 1";
                
    $result = $db->prepare($fieldquery);
    $result->execute();
    foreach($result as $row){
    $id = $row['id'];
        $sql1= "UPDATE `tasks` set `brief` = '".$brief."' WHERE `id` = '".$id."'";
        $db->exec($sql1); 
    }

}elseif( $_POST['action'] == 'edit' ) {
        $sql= "INSERT INTO `log` (`target`,`function`,`userid`,`logged`) VALUES ('test','".$brief."','".$userid."','".$nowx."')";
        $db->exec($sql); 
}

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • cemlimitedcemlimited Posts: 36Questions: 9Answers: 0

    Any help greatly appreciated

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin

    Are you getting any errors from the script? $result = $db->prepare is treating $db as a PDO resource, but if you are using the default $db variable that is created by the DataTables.php include, then it is actually a Database class instance.

    You could use $pdo = $db->resource(); to get the PDO object.

    There is nothing fundamentally wrong with performing SQL queries after the ->json() call, so I don't think that is the issue.

    Allan

Sign In or Register to comment.