Have I over done this? Can I clean up this code any?
I have two states

1) Admin mode
2) Normal mode

in Normal mode, I have two states:

a) student is not in the database
a) student is in the database and has likely reloaded the browser. I'm trying to avoid 
multiple entries with the same $StudentId

 ###############################################
 ### if $_POST["StudentId" is 99999995, we are in Administrative mode
 ### StudentId=99999995 had already been created as id=1
 ### MySQL can only update id=1
 ###############################################
 if($StudentId == "99999995")
 {
 OpenServer();
 $sql = "UPDATE $db.$table SET
  FamilyName      = '$FamilyName',
  FirstName       = '$FirstName',
  HomeFac         = '$HomeFac',
  SessionSelected = '$SessionSelected'
  WHERE StudentId = '99999995'";
 mysql_query($sql) or die(print mysql_error());
 CloseServer();
 }else{
 ###############################################
 ### We are in normal mode
 ###############################################
 OpenServer();
 $sql = "select StudentId from $table where StudentId = ".$StudentId;
 $news = mysql_query($sql) or die(print mysql_error());
  if (mysql_num_rows($news) == 0)
  {
  ###############################################
  ### $_POST["StudentId" has never registered
  ###############################################
  OpenServer();
  $sql = "INSERT INTO $db.$table
  (StudentId,FamilyName,FirstName,HomeFac,SessionSelected)
  values ('$StudentId','$FamilyName','$FirstName','$HomeFac','$SessionSelected')";
  mysql_query($sql) or die(print mysql_error());
  CloseServer();
  }else{
  ###############################################
  ### $StudentId has registered and has likely
  ### reloaded the browser.
  ### This should prevent multiple entries with
  ### the same $StudentId
  ###############################################
  OpenServer();
  $sql = "UPDATE $db.$table SET
   FamilyName      = '$FamilyName',
   FirstName       = '$FirstName',
   HomeFac         = '$HomeFac',
   SessionSelected = '$SessionSelected'
   WHERE StudentId = '$StudentId'";
  mysql_query($sql) or die(print mysql_error());
  CloseServer();
  }
 CloseServer();
 }

function OpenServer()
{
global $server,$user,$pass,$db;
$myconnection = mysql_connect($server,$user,$pass);
mysql_select_db($db,$myconnection);
}

function CloseServer()
{
global $server,$user,$pass;
$myconnection = mysql_connect($server,$user,$pass);
mysql_close($myconnection);
}



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to