Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
Darrell3831
Worker
Worker



Joined: Feb 18, 2004
Posts: 244

PostPosted: Wed Feb 18, 2004 3:31 pm Reply with quote

Hi,

I'm developing a php module for my nuke site and I'd like to ask a question about flow control.

At the end of many functions within nuke you see things like this:

Code:
header("Location: admin.php?op=addStudent&msg=$msg");


From my C backbround I can tell this is a type of branch. Your code continues execution with the supplied function and arguments.

However this is not absolute. It cought me off guard today when these lines were totally ignored and program execution continued to flow through my if statements.

Here's my sample:

Code:
function insertStudent($name, $nukeid, $cid, $status){

   global $prefix, $db;

   $name=filter_text($name, "nohtml");
   $nukeid=intval($nukeid);  // Make sure it's an intiger
   
   $username = isSiteMember($nukeid);
   
   if($username == NULL) {
      $msg = _STUDENT_NOTFOUND;  // Nuke id does not exist
      header("Location: admin.php?op=addStudent&msg=$msg");
   }
   
   $sid = isStudent($nukeid);
   if ($sid) {
      $msg = _STUDENT_ALREADY_IN; // Student is already in university - cant add again
      header("Location: admin.php?op=addStudent&msg=$msg");
   }

   if ($name!="") {
      $username=$name;
   }
   
   $insert = $db->sql_query("INSERT INTO ".$prefix."_university_students VALUES (NULL, '$nukeid', '$username', '$status')");
}



In this bit of code I first expected program execution to skip the sql insertion if certain conditions were met, however that is not the case.

Even when the if statement pulls you in you still don't branch immediately. The sql insert is still done, even though I did not want it. Sad

Do I have a syntax error, or is this not the way to control a programs flow?

Thanks,
Darrell
 
View user's profile Send private message Visit poster's website
Raven
Site Admin/Owner



Joined: Aug 27, 2002
Posts: 17088

PostPosted: Wed Feb 18, 2004 3:48 pm Reply with quote

Kinda-sorta. It actually writes an HTTP header and as such, no code can be written to the browser before it or you will receive the 'headers already sent' message unless output_buffering is on.

I'm not sure exactly where your code fails, but I would rewrite this statement
Code:
if($username == NULL) {
as
Code:
if (isnull($username)) {
 
View user's profile Send private message
Raven







PostPosted: Wed Feb 18, 2004 3:52 pm Reply with quote

Oh, and it is a good idea to add die(); after those header statements Smile
Code:
header("Location: admin.php?op=addStudent&msg=$msg");

die();
 
Darrell3831







PostPosted: Wed Feb 18, 2004 4:01 pm Reply with quote

Thanks for the tip, I rewrote it to if (is_null($username)).

I've been trying to figure this out now for over 7 hours. Something isent right with this, or with my understanding of it.

The code isent exactly failing. I use the two if statements to return an error message to the addStudent function so that the user can give it antoher shot.

They are both actually working. I've put echo statements on every single line and watched my data as I step thru it. I'm droping into the if statements and upon exit of this function I'm going to the appropriate spot..

The problem is that the insert command runs no matter what I do.

If I'm error trapping I don't want the insert command to run on bad data. What am I missing?

I don't believe those header statements are right...
 
Darrell3831







PostPosted: Wed Feb 18, 2004 4:03 pm Reply with quote

OMG,

That fixed it!

Thanks... What a waste of a day...... geessss.
 
Raven







PostPosted: Wed Feb 18, 2004 4:11 pm Reply with quote

I thought that litlle nuance might just do it Wink
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©