Hi there,
I have a php codon module that read a condition and write to the database, but I guess I miss something, as it does not write to the database.
First question: how can I see if it actually read the data (create a log or something)
Secondly is there something missing in this code, that prevent from writing to the database?
{
public function __index()
{
CodonEvent::addListener(‘Aircraft_Condition’);
}
public function EventListener($eventinfo)
{
if($eventinfo[0] == 'pirep_filed')
$pirepinfo = $eventinfo[2];
//Retrieve the aircraft info based on the aircraft id stored in the pirep row
$aircraft = OperationsData::getAircraftByReg($pirepinfo->registration);
//Figure out the $cond that needs to be placed into the
if($pirepinfo->landingrate > -100 && $pirepinfo->landingrate <-500)
$cond = $aircraft->cond -20;
//update the 'cond' column using the aircraft id for row identification
$update = OperationsData::editAircraft(array('id'=>$pirepinfo->aircraftid, 'cond'=>$cond));
}
Well, there is no database code in the sample you posted. You showed a class of some sort.
If you have database code, please post it. If you do not have any, then you should read about
the basics of database access…
Hi Ernie,
I apologise for my lack of knowledge. I have a Virtual Air Force, running on the free software phpVMS ( I am also asking for help here: http://forum.phpvms.net/topic/7037-landing-rates-and-condition/page__st__20 ) PhPVMS is based on codon modules and I am trying to do a small addon that alter the condition based on a landing rate.
*/
class Aircraft_Condition extends CodonModule
{
public function __index()
{
CodonEvent::addListener(‘Aircraft_Condition’);
}
public function EventListener($eventinfo)
{
if($eventinfo[0] == ‘pirep_filed’) {
$pirepinfo = $eventinfo[2];
//Retrieve the aircraft info based on the aircraft id stored in the pirep row
$aircraft = OperationsData::getAircraftByReg($pirepinfo->registration);
//Figure out the $cond that needs to be placed into the
if($pirepinfo->landingrate > -1 && $pirepinfo->landingrate <-500)
$cond = $aircraft->cond -20;
//update the ‘cond’ column using the aircraft id for row identification
$update = $this->updateAircraft($aircraft->id, $cond);
}
}
private function updateAircraft($id, $cond) {
$sql = 'UPDATE '.TABLE_PREFIX.“aircraft
SET cond = ‘$cond’
WHERE id = ‘$id’”;
*/
class Aircraft_Condition extends CodonModule
{
public function __index()
{
CodonEvent::addListener(‘Aircraft_Condition’);
}
public function EventListener($eventinfo)
{
if($eventinfo[0] == ‘pirep_filed’) {
$pirepinfo = $eventinfo[2];
}
}
/**
* Edit an aircraft
*/
public static function editAircraft($data) {
//Retrieve the aircraft info based on the aircraft id stored in the pirep row
$data['registration'] = OperationsData::getAircraftByReg($pirepinfo->registration);
if ($pirepinfo->landingrate > -1 && $pirepinfo->landingrate <-2000) {
$data['cond'] = 50;
}
$sql = "UPDATE " . TABLE_PREFIX . "aircraft";
$sql .= DB::build_update($data);
$sql .= " WHERE `id`={$data['registration']}";
/*$sql = "UPDATE " . TABLE_PREFIX."aircraft
SET ``cond`={$data['cond']},
WHERE `id`={$data['registration']}";*/
$res = DB::query($sql);
if (DB::errno() != 0) return false;
CodonCache::delete('all_aircraft');
CodonCache::delete('all_aircraft_enabled');
CodonCache::delete('all_aircraft_search');
CodonCache::delete('all_aircraft_search_enabled');
return true;
}
Okay, this last post had database code inside it…
[php]
private function updateAircraft($id, $cond) {
$sql = 'UPDATE '.TABLE_PREFIX.“aircraft
SET cond = ‘$cond’
WHERE id = ‘$id’”;
$res = DB::query($sql);
if(DB::errno() != 0)
return false;
return true;
}
[/php]
This line does not look correct:
$sql = 'UPDATE ‘.TABLE_PREFIX.“aircraft
SET cond = ‘$cond’
WHERE id = ‘$id’”;
You are assigning an odd string to the varible $sql. It does not look normal.
Not sure about the VMS system, or the virtual software they are using, but, this is how it should be in a normal PHP program if I was updating it to my database. Unsure if it will work on their system.
$sql = "UPDATE " . TABLE_PREFIX . “aircraft SET cond = ‘$cond’ WHERE id = ‘$id’”;
OR
$sql = "UPDATE " . TABLE_PREFIX . “aircraft SET cond = '” . $cond . "’ WHERE id = ‘" . $id . "’";
I usually use the second version to make the variables stand out from the SQL.
Still not sure if this is your answer, but, I think so. Good luck.
Thank you Ernie, I will try the suggestion you gave. Just for reference, below is a part of the existing working code from the OperationsData.class.php
[php] /**
* Edit an aircraft
*/
public static function editAircraft($data) {