Jump to content

mySQL in Module


Aaron

Recommended Posts

I'm trying to make a booking module, and I don't get any errors, but it doesn't update the flight schedules. I think it has something to do with the mySQL update. Here's the booking.php I havn't made any .tpl and dont think I need to, this is my first module and still figuring it out.

<?php
class Booking extends CodonModule
{
public function index()
{
	$this->set('pilotcode', PilotData::GetPilotCode(Auth::$userinfo->code, Auth::$userinfo->pilotid));
	?>
<p><span style="color: rgb(128,0,0)"><strong><span style="font-size: 36px">Book a Flight</span></strong></span></p>
<form action="http://www.canforce.org/index.php/booking/results" method="post">
	<?php
	if(PilotGroups::group_has_perm(Auth::$usergroups, ACCESS_ADMIN))
	{
	?>
      		<input type="hidden" name="flightnum" value="00<?php echo $pilotid?>" />
	<?php
	}
	else
	{
	?>
       		<input type="hidden" name="flightnum" value="<?php echo $pilotid?>" />;
	<?php
	}
?>
<b>Departure Airport ICAO:</b> <input name="depicao" /><br />
<b>Arrival Airport ICAO:</b> <input name="arricao" /><br />
<b>Aircraft:</b> <select id="aircraft" name="aircraft"> <option selected="selected" value="12">CC-130</option> <option value="13">CC-130J</option> <option value="17">CC-150</option> <option value="4">CC-144</option> <option value="7">CT-142</option> <option value="1">CP-140</option> <option value="20">CC-138</option> <option value="22">CU-170</option> <option value="10">CT-156</option> <option value="8">CC-177</option> <option value="3">CC-115</option> <option value="2">CP-140A</option> <option value="19">CT-114</option> <option value="11">CT-155</option> <option value="14">CF-18</option> <option value="16">F-35</option> <option value="18">CH-124</option> <option value="9">CH-146</option> <option value="6">CH-149</option> <option value="15">CH-139</option> <option value="21">CH-148</option> <option value="5">CH-147D</option></select> <br />
<b>Route:</b> <input name="route" /><br />
<b>Altitude:</b> <input name="flightlevel" /><br />
<input type="submit" value="Submit" /></form>
<?php
}
public function Results()
{
?>
<?php
$con = mysql_connect("xxxx","xxxx","xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("canforce_phpvmsinstall", $con);

mysql_query("
UPDATE phpvms_schedules
SET depicao = '$_POST[depicao]' arricao = '$_POST[arricao]' route = '$_POST[route]' aircraft = '$_POST[aircraft]' flightlevel = '$_POST[flightlevel]'
WHERE flightnum = '$_POST[flightnum]'
");

mysql_close($con)
?>
<?php
}
}
?>

Link to comment
Share on other sites

Probably a good idea to remove those connection details.

You need to put functions in classes, and use the build in database class rather than writing it all out again?

Go take a look at some of the other modules, they refer to functions like PilotData::Blah(); - you need to do this, make your own class and put all the functions in there.

Link to comment
Share on other sites

I'm not sure what you mean Tom. I'm a bit of a noob, I'm ok at editing but suck at creating. I thought it was in a class, "class Booking extends CodonModule". Also, What are the .tpl files for? I understand regular php just fine, its just I dont get how the functions and classes and .tpl work together. Couldn't I just make a module with the functions, and then the index function can be the booking form, then when you click it, it activates another function that updates the database and displays a booking confirmed page. It's a quite simple little module I want to make. I just dont understand the different files I need to make, no admin stuff needed, just to submit the form.

Link to comment
Share on other sites

So now what Ive got is a booking.class.php in the common folder that has the function that updates the database, I put all the stuff that gets the pilots id and that sort of thing in the module booking.php and I made a .tpl that has the html form in it. Now how do I put them together to make them work?

Link to comment
Share on other sites

Still need help, I think I'm getting closer, I need this working by February 4th because thats our first event. Also, not a huge problem, I have it set because admins have ID 00x and pilots have ID 1xx, does anybody know how to get the ID offset and make it so the pilot id is the right length, so instead of being CFC1 it will be CFC001, what I've got now should work but isn't great. Heres the files I made so far.

Core/templates/booking_form.tpl

<?php
//
//@author Aaron Schpitzer
//@copyright Copyright (c) 2009-2012, Aaron Schpitzer
//@license http://creativecommons.org/licenses/by-nc-sa/3.0/
?>
<p><span style="color: rgb(128,0,0)"><strong><span style="font-size: 36px">Book a Flight</span></strong></span></p>
<form action="http://www.canforce.org/index.php/booking" method="post" enctype="multipart/form-data">

	if(PilotGroups::group_has_perm(Auth::$usergroups, ACCESS_ADMIN))
	{
	?>
      		<input type="hidden" name="flightnum" value="00<?php echo $pilotid?>" />
	<?php
	}
	else
	{
	?>
       		<input type="hidden" name="flightnum" value="<?php echo $pilotid?>" />;
	<?php
	}
<b>Departure Airport ICAO:</b> <input name="depicao" /><br />
<b>Arrival Airport ICAO:</b> <input name="arricao" /><br />
<b>Aircraft:</b> <select id="aircraft" name="aircraft"> <option selected="selected" value="12">CC-130 Hercules</option> <option value="13">CC-130J Hercules</option> <option value="17">CC-150 Polaris</option> <option value="4">CC-144 Challenger</option> <option value="7">CT-142 Dash-8</option> <option value="1">CP-140 Aurora</option> <option value="20">CC-138 Twin Otter</option> <option value="22">CU-170 Heron</option> <option value="10">CT-156 Harvard II</option> <option value="8">CC-177 Globemaster III</option> <option value="3">CC-115 Buffalo</option> <option value="2">CP-140A Arcturus</option> <option value="19">CT-114 Tutor</option> <option value="11">CT-155 Hawk</option> <option value="14">CF-18 Hornet</option> <option value="16">F-35 Lightning II</option> <option value="18">CH-124 Sea King</option> <option value="9">CH-146 Griffon</option> <option value="6">CH-149 Cormorant</option> <option value="15">CH-139 Jet Ranger</option> <option value="21">CH-148 Cyclone</option> <option value="5">CH-147D Chinook</option></select> <br />
<b>Route:</b> <input name="route" /><br />
<b>Altitude:</b> <input name="flightlevel" /><br />
      <input type="hidden" name="action" value="save_booking" />
<input type="submit" value="Submit" /></form>

core/common/BookingData.class.php

<?php
//
//@author Aaron Schpitzer
//@copyright Copyright (c) 2009-2012, Aaron Schpitzer
//@license http://creativecommons.org/licenses/by-nc-sa/3.0/

class BookingData extends Codondata {
   public function current_booking($flightnum)
   {
       $query = "SELECT * FROM phpvms_schedules WHERE flightnum='$flightnum'";

       return DB::get_results($query);
   }

    public function save_booking($depicao, $arricao, $route, $aircraft, $flightlevel, $flightnum)
   {
       $query = "UPDATE phpvms_schedules SET
        depicao='$depicao',
        arricao='$arricao',
        route='$route',
        aircraft='$aircraft',
        flightlevel='$flightlevel',
        WHERE flightnum='$flightnum'";

       DB::query($query);
   }

core/modules/booking/booking.php

<?php
//
//@author Aaron Schpitzer
//@copyright Copyright (c) 2009-2012, Aaron Schpitzer
//@license http://creativecommons.org/licenses/by-nc-sa/3.0/

class Booking extends CodonModule
{
   public function index() {
	$this->set('pilotcode', PilotData::GetPilotCode(Auth::$userinfo->code, Auth::$userinfo->pilotid));
	?>
	<?php

           $flightnum = $_POST[flightnum];
           $schedule = array();
           $schedule = BookingData::current_booking($flightnum);
           $this->set('schedule', $schedule);
           $this->show('booking_form.tpl');
   }
   public function save_booking()
   {
       $schedule = array();

       $schedule['flightnum'] = DB::escape($this->post->flightnum);
       $schedule['depicao'] = DB::escape($this->post->depicao);
       $schedule['arricao'] = DB::escape($this->post->arricao);
       $schedule['aircraft'] = DB::escape($this->post->aircraft);
       $schedule['route'] = DB::escape($this->post->route);
       $schedule['flightlevel'] = DB::escape($this->post->flightlevel);

       BookingData::save_booking($schedule['depicao'],
                                 $schedule['arricao'],
                                 $schedule['aircraft'],
                                 $schedule['route'],
                                 $schedule['flightlevel'],
                                 $schedule['flightnum'],

       $this->show('/booking_form.tpl')
   }
   public function thankyou()
{
?>
Your booking was successfully added to our system!
<?php
}
}
?>

IT'S NOT WORKING STILL, GIVES SYNTAX ERROR: Parse error: syntax error, unexpected '}' in /home/canforce/public_html/core/modules/Booking/Booking.php on line 39

ANYBODY KNOW WHERE THIS ERROR COME FROM AND PLZ LET ME KNOW IF U SEE OTHER ERRORS

Link to comment
Share on other sites

The Pilot ID configs are located in the local.config

# See more details about these in the docs
Config::Set('PAGE_EXT', '.htm');	# .htm is fine. You can still run PHP
Config::Set('PILOTID_OFFSET', 0);	# What # to start pilot ID's from
Config::Set('PILOTID_LENGTH', 4);	# Length of the Pilot ID
Config::Set('UNITS', 'nm');			# Your units: nm, mi or km
Config::Set('LOAD_FACTOR', '82');	# %age load factor 
Config::Set('CARGO_UNITS', 'lbs');

Link to comment
Share on other sites

I fixed the module so it is displayed with no error, but it doesn't work :( I think it's most liekely because it cant find the pilot ID, I replaced the code relating to the pilot ID in the booking_form.tpl with the following:

...
<form action="http://www.canforce.org/index.php/booking" method="post" enctype="multipart/form-data">
<?php
	$pilotid = Auth::$userinfo->pilotid;
	$flightnum = PilotData::parsePilotID($pilotid);
?>
      <input type="hidden" name="flightnum" value="<?php echo $flightnum ?>" />

<b>Departure Airport ICAO:</b> <input name="depicao" /><br />
...

Link to comment
Share on other sites

Narrowed down the issues, now everything works fine except for the updating of the database. I know this because I put some "echo $flightnum" and other things on that page and they showed up. So it gets the variables, but isn't updating the database. Here's the relevant code:

    public function save_booking($depicao, $arricao, $route, $aircraft, $flightlevel, $flightnum)
   {
       $query = "UPDATE phpvms_schedules SET
        depicao='$depicao',
        arricao='$arricao',
        route='$route',
        aircraft='$aircraft',
        flightlevel='$flightlevel',
        WHERE flightnum='$flightnum'";

       DB::query($query);
   }
}

Link to comment
Share on other sites

  • 2 weeks later...
Guest lorathon

Why have schedules at all then? Just do charters. What is the difference?

Any how use this. It will at least tell you what is wrong with the query. Be advised that the $aircraft needs to be the aircraft id of the aircraft table.

public function save_booking($depicao, $arricao, $route, $aircraft, $flightlevel, $flightnum)
   {
       $query = "UPDATE phpvms_schedules SET
        `depicao`='$depicao',
        `arricao`='$arricao',
        `route`='$route',
        `aircraft`='$aircraft',
        `flightlevel`='$flightlevel',
        WHERE `flightnum`='$flightnum'";

       DB::query($query);
       echo DB::$error;
   }

Link to comment
Share on other sites

Thank you, here's what it gave me. Is it posible that it doesn't like that my pilot id has 0's in front?

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE flightnum='001'' at line 7

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...