Execute after Update
Execute after Update
cemlimited
Posts: 36Questions: 9Answers: 0
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.
This discussion has been closed.
Answers
Any help greatly appreciated
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 theDataTables.php
include, then it is actually aDatabase
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