Write to database problem

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?

here is the code:
[php]<?php
/**

*/

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 > -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));
		}

}[/php]

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…

Here is one link…
http://www.w3schools.com/php/php_mysql_intro.asp
Click on next-chapter to see more until you have enough… Good luck

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.

here is what I have now:

[php]<?php
/**

*/
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’”;

$res = DB::query($sql);

if(DB::errno() != 0)
return false;

                            return true;

}
}[/php]

ok, with the above not working as well, I have this, and still don’t update.

[php]<?php
/**

*/
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;
}

}[/php]

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) {

    $data['icao'] = DB::escape(strtoupper($data['icao']));
    $data['name'] = DB::escape(strtoupper($data['name']));
    $data['registration'] = DB::escape(strtoupper($data['registration']));

    $data['range'] = ($data['range'] == '') ? 0 : $data['range'];
    $data['weight'] = ($data['weight'] == '') ? 0 : $data['weight'];
    $data['cruise'] = ($data['cruise'] == '') ? 0 : $data['cruise'];

    $data['range'] = str_replace(',', '', $data['range']);
    $data['weight'] = str_replace(',', '', $data['weight']);
    $data['cruise'] = str_replace(',', '', $data['cruise']);
    $data['maxpax'] = str_replace(',', '', $data['maxpax']);
    $data['maxcargo'] = str_replace(',', '', $data['maxcargo']);
    $data['price'] = ($data['price'] == '') ? 0 : $data['price'];
	$data['bought'] = ($data['bought'] == '') ? 0 : $data['bought'];
	$data['cond'] = ($data['cond'] == '') ? 0 : $data['cond'];
	$data['rep'] = ($data['rep'] == '') ? 0 : $data['rep'];


    if (empty($data['minrank'])) {
        $data['minrank'] = 0;
    }

    if ($data['enabled'] === true || $data['enabled'] == '1') $data['enabled'] = 1;
    else  $data['enabled'] = 0;

    if ($data['minrank'] > 0) {
        $data['ranklevel'] = RanksData::getRankLevel($data['minrank']);
    } else {
        $data['ranklevel'] = 0;
    }

    $sql = "UPDATE " . TABLE_PREFIX . "aircraft SET ";
    $sql .= DB::build_update($data);
    $sql .= " WHERE `id`={$data['id']}";

    /*$sql = "UPDATE " . TABLE_PREFIX."aircraft
    SET `icao`='{$data['icao']}', `name`='{$data['name']}', `fullname`='{$data['fullname']}',
    `registration`='{$data['registration']}', `downloadlink`='{$data['downloadlink']}', 
    `imagelink`='{$data['imagelink']}', `range`='{$data['range']}', `weight`='{$data['weight']}',
    `cruise`='{$data['cruise']}', `maxpax`='{$data['maxpax']}', `maxcargo`='{$data['maxcargo']}',
    `minrank`={$data['minrank']}, `ranklevel`={$rank_level}, `enabled`={$data['enabled']}, `bought`={$data['bought']}, `cond`={$data['cond']},`rep`={$data['rep']}
    WHERE `id`={$data['id']}";*/

    $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;
}[/php]

Okay, in the last post, this line: $sql .= " WHERE id={$data[‘id’]}";
Should be: $sql .= " WHERE id = {$data[‘id’]}";

Tables inside SQL WHERE’s are not quoted…

Sponsor our Newsletter | Privacy Policy | Terms of Service