Jump to content

Error when I tried to add a field for the ranks


Gofast77

Recommended Posts

Hi guys,

There are some days I wanted to add fields for the ranks (description, etc) but I got this error : Error updating the rank: 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 'Pilot' at line 3

Can you help me ?

Thanks, Gofast

Link to comment
Share on other sites

Can you please copy down your SQL query? Usually it has something to do with the misconception a lot of people have of when to use ` and '. `` is used around SQL variables, while '' is used around php variables. As a matter of fact, none of them are really necessary but do a lot to toughen up protection against SQL injection.

Link to comment
Share on other sites

class RanksData extends CodonData
{
 public function getranks()
 {
  $sql = "SELECT * FROM " . TABLE_PREFIX . "ranks ORDER BY minhours";
  return DB::get_results($sql);
 }
 public function getaircrafts($rankid)
 {
  $sql= "SELECT distinct icao FROM " . TABLE_PREFIX . "aircraft WHERE minrank=" . $rankid;
  return DB::get_results($sql);
 }

 static $lasterror;

 /**
  * Return information about the rank, given the ID
  */
 public static function getRankInfo($rankid)
 {
  $sql = 'SELECT * FROM '.TABLE_PREFIX.'ranks
  WHERE rankid='.$rankid;

  return DB::get_row($sql);
 }

 /**
  * Returns all the ranks, and the total number of pilots
  * on each rank
  */
 public static function getAllRanks()
 {
  $allranks = CodonCache::read('all_ranks');

  if($allranks === false)
  {
   $sql = 'SELECT r.*, (SELECT COUNT(*) FROM '.TABLE_PREFIX.'pilots WHERE rank=r.rank) as totalpilots
  FROM ' .TABLE_PREFIX.'ranks r
  ORDER BY r.minhours ASC';

   $allranks = DB::get_results($sql);
   CodonCache::write('all_ranks', $allranks, 'long');
  }

  return $allranks;
 }

 public static function getRankImage($rank)
 {
  $sql = 'SELECT `rankimage` FROM '.TABLE_PREFIX.'ranks WHERE rank="'.$rank.'"';
  return DB::get_var($sql);
 }

 /**
  * Get the level the passed rank is in the list
  */
 public static function getRankLevel($rankid)
 {
  if($rankid == 0) { return 0; }
  $all_ranks = self::getAllRanks();

  $i=0;
  foreach($all_ranks as $rank)
  {
   $i++;

   if($rank->rankid == $rankid)
   {
 return $i;
   }
  }

  return 0;
 }

 /**
  * Give the number of hours, return the next rank
  */
 public static function getNextRank($hours)
 {
  $sql = "SELECT * FROM ".TABLE_PREFIX."ranks
  WHERE minhours>$hours ORDER BY minhours ASC LIMIT 1";

  return DB::get_row($sql);
 }

 /**
  * Add a ranking. This will automatically call
  * CalculatePilotRanks() at the end
  */
 public static function addRank($title, $minhours, $imageurl, $description, $payrate, $exams, $briefing)
 {
  $minhours = intval($minhours);
  $payrate = floatval($payrate);

  $sql = "INSERT INTO ".TABLE_PREFIX."ranks (rank, rankimage, minhours, description, payrate, exams, briefing)
  VALUES('$title', '$imageurl', '$description', '$minhours', '$payrate','$exams', '$briefing')";

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

  if(DB::$errno == 1062)
  {
   self::$lasterror = 'This already exists';
   return false;
  }

  CodonCache::delete('all_ranks');
  self::CalculatePilotRanks();

  return true;
 }

 /**
  * Update a certain rank
  */
 public static function updateRank($rankid, $title, $minhours, $imageurl, $description, $payrate, $exams, $briefing)
 {
  $minhours = intval($minhours);
  $payrate = floatval($payrate);

  $sql = "UPDATE ".TABLE_PREFIX."ranks
  SET rank='$title', rankimage='$imageurl', minhours='$minhours', description='$description', payrate='$payrate', exams='$exams', briefing='$briefing'
  WHERE rankid=$rankid";

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

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

  CodonCache::delete('all_ranks');

  self::CalculatePilotRanks();
  return true;
 }

 /**
  * Delete a rank, and then recalculate
  */

 public static function deleteRank($rankid)
 {
  $sql = 'DELETE FROM '.TABLE_PREFIX.'ranks
  WHERE rankid='.$rankid;

  DB::query($sql);

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

  CodonCache::delete('all_ranks');
  self::CalculatePilotRanks();
  return true;
 }

 /**
  * Go through each pilot, check their hours, and see where they
  *  stand in the rankings. If they are above the minimum hours
  *  for that rank level, then make $last_rank that text. At the
  *  end, update that
  */
 public static function calculatePilotRanks()
 {
  /* Don't calculate a pilot's rank if this is set */
  if(Config::Get('RANKS_AUTOCALCULATE') === false)
  {
   return;
  }

  $allranks = self::getAllRanks();
  $pilots = PilotData::getAllPilots();
  if(count($pilots) == 0 || !is_array($pilots))
  {
   return;
  }

  foreach($pilots as $pilot)
  {
   $last_rank = '';

   $pilothours = intval($pilot->totalhours);
   if(Config::Get('TRANSFER_HOURS_IN_RANKS') == true)
   {
 $pilothours += $pilot->transferhours;
   }

   $i = 1;
   foreach($allranks as $rank)
   {
 if($pilothours >= intval($rank->minhours))
 {
  $rank_level = $i;
  $last_rank = $rank->rank;
  $last_rankid = $rank->rankid;
 }

 $i++;
   }

   $update = array(
 'rankid' => $last_rankid,
 'rank' => $last_rank,
 'ranklevel' => $rank_level,
   );

   PilotData::updateProfile($pilot->pilotid, $update);
  }
 }

 public static function calculateUpdatePilotRank($pilotid)
 {
  /* Don't calculate a pilot's rank if this is set */
  if(Config::Get('RANKS_AUTOCALCULATE') == false)
  {
   return;
  }

  $pilotid = intval($pilotid);
  $allranks = self::GetAllRanks();
  $pilot = PilotData::GetPilotData($pilotid);
  $pilothours = $pilot->totalhours;

  if(Config::Get('TRANSFER_HOURS_IN_RANKS') == true)
  {
   $pilothours += $pilot->transferhours;
  }

  $i = 0;
  foreach($allranks as $rank)
  {
   $i++;

   if($pilothours >= intval($rank->minhours))
   {
 $rank_level = $i;
 $last_rank = $rank->rank;
 $last_rankid = $rank->rankid;
   }
  }

  $update = array(
   'rankid' => $last_rankid,
   'rank' => $last_rank,
   'ranklevel' => $rank_level,
  );

  PilotData::updateProfile($pilot->pilotid, $update);
 }
}

Link to comment
Share on other sites

Try the following replacement for that $sql over to where you added the description parameter.

$sql = "UPDATE `".TABLE_PREFIX."ranks`

SET `rank`='$title', `rankimage`='$imageurl', `minhours`='$minhours', `description`='$description', `payrate`='$payrate', `exams`='$exams', `briefing`='$briefing'

WHERE `rankid`='$rankid'";

Also make sure that wherever you're passing in these parameters(Probably the admin module for ranks), you have the same number of arguments as parameters.

Parameters are the variables in the function($var1, $var2) where the function does something.

Arguments are the variables in the function($var1, $var2) where you're calling the function that does something.

Link to comment
Share on other sites

Excuse me I have two website (one in French and one in English, I confused the two). The SQL error exists more but the other if and when I change, there is nothing changes...

I don't think you understand what I mean, which probably means you didn't modify the argument function. The argument function is found in the admin/core/modules folder under whichever module you added the 'description' input box. Right there when you find the if(this->post->action == 'editRank') or something of the sort..., you should also find a statement to be executed that refers to the function that you have modified. It should look like:

myFunction($this->post->var1, $this->post->var2, $this->post->var3, ...)

Be sure that $this->post->description is found in that function in the correct order as the parameter function found in the code above:

public static function updateRank($rankid, $title, $minhours, $imageurl, $description, $payrate, $exams, $briefing)

Link to comment
Share on other sites

That's it, thanks.

I wanted to add one final fields and I have this error : Error updating the rank: 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 't fly without the presence of an instructor.', `payrate`='25', `exams`='-', `bri' at line 2

Link to comment
Share on other sites

That's it, thanks.

I wanted to add one final fields and I have this error : Error updating the rank: 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 't fly without the presence of an instructor.', `payrate`='25', `exams`='-', `bri' at line 2

Can you copy&paste that edited SQL statement please? Just the SQL Statement you modified.

Link to comment
Share on other sites

public static function updateRank($rankid, $title, $minhours, $imageurl, $conditions, $description, $payrate, $exams, $briefing)
 {
  $minhours = intval($minhours);
  $payrate = floatval($payrate);

  $sql = "UPDATE `".TABLE_PREFIX."ranks`
	  SET `rank`='$title', `rankimage`='$imageurl', `minhours`='$minhours', `conditions`='$conditions', `description`='$description', `payrate`='$payrate', `exams`='$exams', `briefing`='$briefing'
	  WHERE `rankid`='$rankid'";

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

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

  CodonCache::delete('all_ranks');

  self::CalculatePilotRanks();
  return true;
 }

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...