[PHP] Have I over done this?

2003-08-16 Thread John Taylor-Johnston
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 9995, we are in Administrative mode
 ### StudentId=9995 had already been created as id=1
 ### MySQL can only update id=1
 ###
 if($StudentId == "9995")
 {
 OpenServer();
 $sql = "UPDATE $db.$table SET
  FamilyName  = '$FamilyName',
  FirstName   = '$FirstName',
  HomeFac = '$HomeFac',
  SessionSelected = '$SessionSelected'
  WHERE StudentId = '9995'";
 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



RE: [PHP] Have I over done this?

2003-08-18 Thread Chris W. Parker
John Taylor-Johnston 
on Saturday, August 16, 2003 5:19 PM said:

> Have I over done this?

That I don't know.

> Can I clean up this code any?

Yes.

1. You can take your open/closeserver() functions one step farther and
create a query function (can be called something like dbQuery()).
Anytime you repeat code you should look into a way of turning that
repeating code into a function. For example you seem to follow this:

OpenServer();
$sql = "SELECT mystuff";
mysql_query($sql) or die(..);
CloseServer();

Turn that into a function instead and save yourself the hassle of typing
mysql_query(..) or die(..);


Function myQuery($sql)
{
OpenServer();
mysql_query($sql) or die(..);
CloseServer();
}

Now all you have to do is this:

$sql = "SELECT mystuff";
myQuery($sql);

2. Only because it's redundant, you shouldn't Open/CloseServer() in each
code block. Instead, OpenServer() and the beginning of the page and
CloseServer() at the end of the page.




That would turn the function I wrote above into this:

Function myQuery($sql)
{
mysql_query($sql) or die(..);
}

3. In my limited experience with php I would recommend you create a
database class that will organize all your db functions into one object.
Shoot me an email offlist if you'd like to see what I've written.



HTH,
Chris.

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