# Write to database problem

**URL:** <https://www.phphelp.com/t/write-to-database-problem/16325>\
**Category:** General PHP Help\
**Created:** [March 21, 2012, 11:30am UTC](https://www.phphelp.com/t/write-to-database-problem/16325 "2012-03-21T11:30:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakes](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/2acd7d/32.png) [@Jakes](https://www.phphelp.com/u/Jakes)\
**Post date:** [March 21, 2012, 11:30am UTC](https://www.phphelp.com/t/write-to-database-problem/16325/1 "2012-03-21T11:30:21Z")

</div>

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  
/\*\*

- phpVMS - Virtual Airline Administration Software
- Copyright © 2008 Nabeel Shahzad
- For more information, visit [www.phpvms.net](http://www.phpvms.net)
- Forums: [http://www.phpvms.net/forum](http://www.phpvms.net/forum)
- Documentation: [http://www.phpvms.net/docs](http://www.phpvms.net/docs)
- 
- phpVMS is licenced under the following license:
- Creative Commons Attribution Non-commercial Share Alike (by-nc-sa)
- View license.txt in the root, or visit [http://creativecommons.org/licenses/by-nc-sa/3.0/](http://creativecommons.org/licenses/by-nc-sa/3.0/)
- 

\*/

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]

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [March 21, 2012, 3:08pm UTC](https://www.phphelp.com/t/write-to-database-problem/16325/2 "2012-03-21T15:08:51Z")

</div>

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](http://www.w3schools.com/php/php_mysql_intro.asp)  
Click on next-chapter to see more until you have enough… Good luck

---

<div class="post-metadata">

**Author:** ![Jakes](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/2acd7d/32.png) [@Jakes](https://www.phphelp.com/u/Jakes)\
**Post date:** [March 22, 2012, 7:35am UTC](https://www.phphelp.com/t/write-to-database-problem/16325/3 "2012-03-22T07:35:08Z")

</div>

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](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  
/\*\*

- phpVMS - Virtual Airline Administration Software
- Copyright © 2008 Nabeel Shahzad
- For more information, visit [www.phpvms.net](http://www.phpvms.net)
- Forums: [http://www.phpvms.net/forum](http://www.phpvms.net/forum)
- Documentation: [http://www.phpvms.net/docs](http://www.phpvms.net/docs)
- 
- phpVMS is licenced under the following license:
- Creative Commons Attribution Non-commercial Share Alike (by-nc-sa)
- View license.txt in the root, or visit [http://creativecommons.org/licenses/by-nc-sa/3.0/](http://creativecommons.org/licenses/by-nc-sa/3.0/)
- 

\*/  
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]

---

<div class="post-metadata">

**Author:** ![Jakes](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/2acd7d/32.png) [@Jakes](https://www.phphelp.com/u/Jakes)\
**Post date:** [March 22, 2012, 9:32am UTC](https://www.phphelp.com/t/write-to-database-problem/16325/4 "2012-03-22T09:32:31Z")

</div>

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

[php]\<?php  
/\*\*

- phpVMS - Virtual Airline Administration Software
- Copyright © 2008 Nabeel Shahzad
- For more information, visit [www.phpvms.net](http://www.phpvms.net)
- Forums: [http://www.phpvms.net/forum](http://www.phpvms.net/forum)
- Documentation: [http://www.phpvms.net/docs](http://www.phpvms.net/docs)
- 
- phpVMS is licenced under the following license:
- Creative Commons Attribution Non-commercial Share Alike (by-nc-sa)
- View license.txt in the root, or visit [http://creativecommons.org/licenses/by-nc-sa/3.0/](http://creativecommons.org/licenses/by-nc-sa/3.0/)
- 

\*/  
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]

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [March 22, 2012, 8:12pm UTC](https://www.phphelp.com/t/write-to-database-problem/16325/5 "2012-03-22T20:12:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jakes](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/2acd7d/32.png) [@Jakes](https://www.phphelp.com/u/Jakes)\
**Post date:** [March 23, 2012, 5:37am UTC](https://www.phphelp.com/t/write-to-database-problem/16325/6 "2012-03-23T05:37:52Z")

</div>

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]
```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [March 23, 2012, 2:59pm UTC](https://www.phphelp.com/t/write-to-database-problem/16325/7 "2012-03-23T14:59:43Z")

</div>

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…
