Jump to content

Not working


topflight

Recommended Posts

This code it suppose to show errors if the fileds are empty and also if everyihng is ok it is suppose to insert the information in the database and shoot an email to the new member. Well it is not working and it is saying $error is an undefined vaiable on line 12 which is if (!$_POST['fname']){ $error .='No First name<br>';  please help. Thanks in advanced!

 

 

<?
include 'db.php';
session_start();
if(isset($_SESSION['LOGGEDIN'])==TRUE){
echo'You are already a member';}

if(isset($_POST['apply'])){




if (!$_POST['fname']){ $error .='No First name<br>'; }
if (!$_POST['lname']){ $error .='No Last  name<br>'; }
if (!$_POST['email']){ $error .='No Email Address<br>'; }
if (!$_POST['vatsimid']){ $error .='No VATSIM ID<br>'; }
if (!$_POST['hub']){ $error .='No Hub<br>'; }
if (!$_POST['pwd']){ $error .='No Password<br>'; }
if (!$_POST['pwdc']){ $error .='No Password Conformation<br>'; }
if (!$_POST['emailc']){ $error .='No Conformation Email Address<br>'; }
if ($_POST['thours']){ if (!$_POST['thoursc']){ $error .='No comments<br>'; }}
if ($_POST['email'] != $_POST['emailc']){ $error .='Email mismatch'; }
if ($_POST['pwd'] != $_POST['pwdc']){ $error .='Password mismatch'; }

if ($error){ echo $error;?> <FORM><INPUT TYPE="BUTTON" VALUE="Fix Errors" 
ONCLICK="history.go(-1)"></FORM> <? } else {

$cerror = count($error);
$fpwd= $_POST['pwd'];
$pwd = md5($fpwd);
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$pilotl= mysql_query("SELECT * FROM `pilots` WHERE hub='$_POST[hub]' ORDER BY login DESC LIMIT 1") or die(mysql_error());

$plrows = mysql_num_rows($pilotl);
if ($plrows=="0"){ 
if ($_POST['hub']=="KPDX"){ $login = "1000"; }
if ($_POST['hub']=="KLAX"){ $login = "2000"; }
if ($_POST['hub']=="PANC"){ $login = "3000"; }
if ($_POST['hub']=="KSEA"){ $login = "4000"; }} else {
if($cerror =='0'){
while($plr = mysql_fetch_array($pilotl)){ $login = $plr[login]; } $login = $login + 1;  

$update = mysql_query("INSERT INTO pilots (login,pwd,fname,lname,email,hub,thours,thoursc,rating,vatsimid,date,ip,status,hm,ed,bm,active) VALUES ('$login','$pwd','$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[hub]','$_POST[thours]','$_POST[thoursc]','First Officer','$_POST[vatsimid]','$now','$ip','0','0','0',0,0)") or die(mysql_error());


$jto = "email";
$jsubject = "A new pilot has signed up";
$jjmessae = "
A new pilot ($fname $laname) with the pending ID of ($login) has submitted an Applications with --------. Please activate their test. ";
$from = "email";
$jheaders = "From: $from";

mail($jto, $jsubject, $jmessage, $jheaders);

$to = "$email";
$subject = "RE:------ Application ($fname $lname)";
$messae = "
Dear ($fname $lname),

We have reccevied your application for membership with -----. You will reccevie another email within 24-48 hours regarding your entry exam. It is really importaint that you complete this exam ASAP. In order to have a membership account with Simulated Alaska you MUST pass this exam with an 80%. So in the meantime please look at the SOP's, explore the site, and study basic aircraft Maneuvers. The exam will cover(Simulated Alaska Operations, Basic Aircraft Maneuvers, and Basic Flying Skills. Once you have pass this test you will then recceive another email saying you have been accepted.




Sincerly,
--------
";

$from = "email";
$headers = "From: $from";
mail($to, $subject, $message, $headers);


echo'<b>Application Sent ------- Staff</b>';

} } } }

?>

Link to comment
Share on other sites

He means, change

<?
include 'db.php';
session_start();
if(isset($_SESSION['LOGGEDIN'])==TRUE){
echo'You are already a member';}
?>

 

to

 

<?
include 'db.php';
session_start();
$error = '';
if(isset($_SESSION['LOGGEDIN'])==TRUE){
echo'You are already a member';}
?>

Link to comment
Share on other sites

Ok I've found the problem.  It's your indentation.  You need to write your code like this:

 

if (something) {
  do something
} else {
  do something else
}

 

Notice how everything inside is indented, making it clear what runs only if the "if" is matched.  When you fix your indentation, the problem with your code will be obvious.

Link to comment
Share on other sites

topflight, the code you posted looks like this:

 

if (something) {
do something
} else {
if (somethingelse) {
do something else
}
}

 

What it should look like is this:

 

if (something) {
    do something
} else {
    if (somethingelse) {
        do something else
    }
}

 

The second version makes it very easy to see what is inside each "if" and what is not.

 

 

Link to comment
Share on other sites

Now my code looks like this:

 

<?
include 'db.php';
session_start();
if(isset($_SESSION['LOGGEDIN'])==TRUE){
echo'You are already a member ';}

if(isset($_POST['apply'])){




if (!$_POST['fname']){ $error .='No First name<br>'; }
if (!$_POST['lname']){ $error .='No Last  name<br>'; }
if (!$_POST['email']){ $error .='No Email Address<br>'; }
if (!$_POST['vatsimid']){ $error .='No VATSIM ID<br>'; }
if (!$_POST['hub']){ $error .='No Hub<br>'; }
if (!$_POST['pwd']){ $error .='No Password<br>'; }
if (!$_POST['pwdc']){ $error .='No Password Conformation<br>'; }
if (!$_POST['emailc']){ $error .='No Conformation Email Address<br>'; }
if ($_POST['thours']){ if (!$_POST['thoursc']){ $error .='No comments<br>'; }}
if ($_POST['email'] != $_POST['emailc']){ $error .='Email mismatch'; }
if ($_POST['pwd'] != $_POST['pwdc']){ $error .='Password mismatch'; }

if ($error)
  { echo $error;?> <FORM><INPUT TYPE="BUTTON" VALUE="Fix Errors" 
ONCLICK="history.go(-1)"></FORM> <? 
   
   } else {

         $cerror = count($error);
         $fpwd= $_POST['pwd'];
         $pwd = md5($fpwd);
         $fname = $_POST['fname'];
         $lname = $_POST['lname'];
         $email = $_POST['email'];
         $pilotl= mysql_query("SELECT * FROM `pilots` WHERE hub='$_POST[hub]' ORDER BY login DESC LIMIT 1") or die(mysql_error());

         $plrows = mysql_num_rows($pilotl);
if ($plrows=="0"){ 
if ($_POST['hub']=="KPDX"){ 
    $login = "1000"; }
if ($_POST['hub']=="KLAX"){ 
    $login = "2000"; }
if ($_POST['hub']=="PANC"){ 
    $login = "3000"; }
if ($_POST['hub']=="KSEA"){ 
    $login = "4000"; }
} else {
       if($cerror =='0'){

  while($plr = mysql_fetch_array($pilotl)){ $login = $plr[login]; } $login = $login + 1;  

$update = mysql_query("INSERT INTO `pilots` (login,pwd,fname,lname,email,hub,thours,thoursc,rating,vatsimid,date,ip,status,hm,ed,bm,active) VALUES ('$login','$pwd','$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[hub]','$_POST[thours]','$_POST[thoursc]','First Officer','$_POST[vatsimid]','$now','$ip','0','0','0',0,0)") or die(mysql_error());


$jto = "jobs@simulatedasa.org";
$jsubject = "A new pilot has signed up";
$jjmessae = "
A new pilot ($fname $laname) with the pending ID of ($login) has submitted an Applications with Simulated Alaska. Please activate their test. ";
$from = "jobs@simulatedasa.org";
$jheaders = "From: $from";

mail($jto, $jsubject, $jmessage, $jheaders);

$jto = "email";
$jsubject = "A new pilot has signed up";
$jjmessae = "
A new pilot ($fname $laname) with the pending ID of ($login) has submitted an Applications with --------. Please activate their test. ";
$from = "email";
$jheaders = "From: $from";

mail($jto, $jsubject, $jmessage, $jheaders);

$to = "$email";
$subject = "RE:------ Application ($fname $lname)";
$messae = "
Dear ($fname $lname),

We have reccevied your application for membership with -----. You will reccevie another email within 24-48 hours regarding your entry exam. It is really importaint that you complete this exam ASAP. In order to have a membership account with Simulated Alaska you MUST pass this exam with an 80%. So in the meantime please look at the SOP's, explore the site, and study basic aircraft Maneuvers. The exam will cover(Simulated Alaska Operations, Basic Aircraft Maneuvers, and Basic Flying Skills. Once you have pass this test you will then recceive another email saying you have been accepted.




Sincerly,
--------
";

$from = "email";
$headers = "From: $from";
mail($to, $subject, $message, $headers);


echo'<b>Application Sent ------- Staff</b>';

} } } }

?>

Link to comment
Share on other sites

Hi

 

This is with the indentation sorted out to make the flow more obvious.

 

<?php
include 'db.php';
$error = '';
session_start();
if(isset($_SESSION['LOGGEDIN'])==TRUE){
echo'You are already a member ';}

if(isset($_POST['apply']))
{
if (!$_POST['fname']){ $error .='No First name<br>'; }
if (!$_POST['lname']){ $error .='No Last  name<br>'; }
if (!$_POST['email']){ $error .='No Email Address<br>'; }
if (!$_POST['vatsimid']){ $error .='No VATSIM ID<br>'; }
if (!$_POST['hub']){ $error .='No Hub<br>'; }
if (!$_POST['pwd']){ $error .='No Password<br>'; }
if (!$_POST['pwdc']){ $error .='No Password Conformation<br>'; }
if (!$_POST['emailc']){ $error .='No Conformation Email Address<br>'; }
if ($_POST['thours']){ if (!$_POST['thoursc']){ $error .='No comments<br>'; }}
if ($_POST['email'] != $_POST['emailc']){ $error .='Email mismatch'; }
if ($_POST['pwd'] != $_POST['pwdc']){ $error .='Password mismatch'; }

if ($error)
{ 
	echo $error;?> <FORM><INPUT TYPE="BUTTON" VALUE="Fix Errors" ONCLICK="history.go(-1)"></FORM> <?php 
} 
else 
{
	$cerror = count($error);
	$fpwd= $_POST['pwd'];
	$pwd = md5($fpwd);
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$email = $_POST['email'];
	$pilotl= mysql_query("SELECT * FROM `pilots` WHERE hub='$_POST[hub]' ORDER BY login DESC LIMIT 1") or die(mysql_error());

	$plrows = mysql_num_rows($pilotl);
	if ($plrows=="0")
	{ 
		if ($_POST['hub']=="KPDX")
		{ 
			$login = "1000"; 
		}
		if ($_POST['hub']=="KLAX")
		{ 
			$login = "2000"; 
		}
		if ($_POST['hub']=="PANC")
		{ 
			$login = "3000"; 
		}
		if ($_POST['hub']=="KSEA")
		{ 
			$login = "4000"; 
		}
	} 
	else 
	{
		if($cerror =='0')
		{

			while($plr = mysql_fetch_array($pilotl))
			{ 
				$login = $plr[login]; 
			} 

			$login = $login + 1;  

			$update = mysql_query("INSERT INTO `pilots` (login,pwd,fname,lname,email,hub,thours,thoursc,rating,vatsimid,date,ip,status,hm,ed,bm,active) VALUES ('$login','$pwd','$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[hub]','$_POST[thours]','$_POST[thoursc]','First Officer','$_POST[vatsimid]','$now','$ip','0','0','0',0,0)") or die(mysql_error());

			$jto = "jobs@simulatedasa.org";
			$jsubject = "A new pilot has signed up";
			$jjmessae = "
			A new pilot ($fname $laname) with the pending ID of ($login) has submitted an Applications with Simulated Alaska. Please activate their test. ";
			$from = "jobs@simulatedasa.org";
			$jheaders = "From: $from";

			mail($jto, $jsubject, $jmessage, $jheaders);

			$jto = "email";
			$jsubject = "A new pilot has signed up";
			$jjmessae = "
			A new pilot ($fname $laname) with the pending ID of ($login) has submitted an Applications with --------. Please activate their test. ";
			$from = "email";
			$jheaders = "From: $from";

			mail($jto, $jsubject, $jmessage, $jheaders);

			$to = "$email";
			$subject = "RE:------ Application ($fname $lname)";
			$messae = "
	Dear ($fname $lname),

	We have reccevied your application for membership with -----. You will reccevie another email within 24-48 hours regarding your entry exam. It is really importaint that you complete this exam ASAP. In order to have a membership account with Simulated Alaska you MUST pass this exam with an 80%. So in the meantime please look at the SOP's, explore the site, and study basic aircraft Maneuvers. The exam will cover(Simulated Alaska Operations, Basic Aircraft Maneuvers, and Basic Flying Skills. Once you have pass this test you will then recceive another email saying you have been accepted.




	Sincerly,
	--------
	";

			$from = "email";
			$headers = "From: $from";
			mail($to, $subject, $message, $headers);
			echo'<b>Application Sent ------- Staff</b>';
		} 
	} 
} 
}

?>

 

An obvious bug is that you set up a variable called $messae but then try and use $message to email (same for $jmessage / $jmessae and $jjmessage / $jjmessae).

 

All the best

 

Keith

Link to comment
Share on other sites

Also I fixed that email thing to:

 

$jto = "";

$jsubject = "";

$jmessae = ". ";

$from = "";

$jheaders = "From: $from";

 

mail($jto, $jsubject, $jmessage, $jheaders);

 

$to = "$email";

$subject = "";

$message = ""

$from = "";

$headers = "From: $from";

mail($to, $subject, $message, $headers);

 

Also of course in my code the fields have data in it. and by the way I am sending to emails one to admin and one to the new member. I am also not receiving the undefined error no more but I am getting a white page when everything is ok.

 

 

Link to comment
Share on other sites

Hi

 

Another error I have seen is that you get all the pilots and then loop through them until the last one, assigning $login, then once you have looped through them all you add one to $login and use that as the key. Couple of issues here. If 2 people at the same precise moment signed up you could get a duplicate, and more importantly you get all the pilots in descending order of login. So if you had 100 of them you would loop down from 100 to 1, then add 1 to 1 getting 2 and then try and insert the new pilot as login 2 (which probably already exists).

 

At the very least take the DESC off the select of all the logins. Preferably change the structure of the table such that login is a key field and set up as "autonumber" (and in this way you could just insert to the table without specifying the login field and mysql would generate it for you), although this would upset your idea of having different ranges of login for different values of hub.

 

Lastly you are checking $error and putting out a message if it is set. You then check for a count of the number of elements in the array $error (which isn't an array). My guess is that php is deciding that $error being a string is just an array of 1 element and putting 1 into $cerror, and so bypasses the whole emain step.

 

All the best

 

Keith

Link to comment
Share on other sites

My code is a basic registration script. First it checks to make sure if all the fields are not empty if they are not then it will display an error message for that field or fields that is empty.  Next it will md5 the password then it will give the user a login ID depending on the hub they choose. Then it adds one to the ID so if their is a 1000 id number in the database it will make the new member ID number 1001. After that it should insert it into the database. Then email the new member letting them know what happening to their account and also an admin letting them know they have to activate a new member. Here is my code:

<?php
include 'db.php';
$error = '';
session_start();
if(isset($_SESSION['LOGGEDIN'])==TRUE){
echo'You are already a member ';}

if(isset($_POST['apply']))
{
   if (!$_POST['fname']){ $error .='No First name<br>'; }
   if (!$_POST['lname']){ $error .='No Last  name<br>'; }
   if (!$_POST['email']){ $error .='No Email Address<br>'; }
   if (!$_POST['vatsimid']){ $error .='No VATSIM ID<br>'; }
   if (!$_POST['hub']){ $error .='No Hub<br>'; }
   if (!$_POST['pwd']){ $error .='No Password<br>'; }
   if (!$_POST['pwdc']){ $error .='No Password Conformation<br>'; }
   if (!$_POST['emailc']){ $error .='No Conformation Email Address<br>'; }
   if ($_POST['thours']){ if (!$_POST['thoursc']){ $error .='No comments<br>'; }}
   if ($_POST['email'] != $_POST['emailc']){ $error .='Email mismatch'; }
   if ($_POST['pwd'] != $_POST['pwdc']){ $error .='Password mismatch'; }

   if ($error)
   { 
      echo $error;?> <FORM><INPUT TYPE="BUTTON" VALUE="Fix Errors" ONCLICK="history.go(-1)"></FORM> <?php 
   } 
   else 
   {
      $cerror = count($error);
      $fpwd= $_POST['pwd'];
      $pwd = md5($fpwd);
      $fname = $_POST['fname'];
      $lname = $_POST['lname'];
      $email = $_POST['email'];
      $pilotl= mysql_query("SELECT * FROM `pilots` WHERE hub='$_POST[hub]' ORDER BY login DESC LIMIT 1") or die(mysql_error());

      $plrows = mysql_num_rows($pilotl);
      if ($plrows=="0")
      { 
         if ($_POST['hub']=="KPDX")
         { 
            $login = "1000"; 
         }
         if ($_POST['hub']=="KLAX")
         { 
            $login = "2000"; 
         }
         if ($_POST['hub']=="PANC")
         { 
            $login = "3000"; 
         }
         if ($_POST['hub']=="KSEA")
         { 
            $login = "4000"; 
         }
      } 
      else 
      {
         if($cerror =='0')
         {

            while($plr = mysql_fetch_array($pilotl))
            { 
               $login = $plr[login]; 
            } 
            
            $login = $login + 1;  

            $update = mysql_query("INSERT INTO `pilots` (login,pwd,fname,lname,email,hub,thours,thoursc,rating,vatsimid,date,ip,status,hm,ed,bm,active) VALUES ('$login','$pwd','$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[hub]','$_POST[thours]','$_POST[thoursc]','First Officer','$_POST[vatsimid]','$now','$ip','0','0','0',0,0)") or die(mysql_error());

            $jto = "jobs@simulatedasa.org";
            $jsubject = "A new pilot has signed up";
            $jjmessae = "
            A new pilot ($fname $laname) with the pending ID of ($login) has submitted an Applications with Simulated Alaska. Please activate their test. ";
            $from = "jobs@simulatedasa.org";
            $jheaders = "From: $from";

            mail($jto, $jsubject, $jmessage, $jheaders);

            $jto = "email";
            $jsubject = "A new pilot has signed up";
            $jjmessae = "
            A new pilot ($fname $laname) with the pending ID of ($login) has submitted an Applications with --------. Please activate their test. ";
            $from = "email";
            $jheaders = "From: $from";

            mail($jto, $jsubject, $jmessage, $jheaders);

            $to = "$email";
            $subject = "RE:------ Application ($fname $lname)";
            $messae = "
      Dear ($fname $lname),

      We have reccevied your application for membership with -----. You will reccevie another email within 24-48 hours regarding your entry exam. It is really importaint that you complete this exam ASAP. In order to have a membership account with Simulated Alaska you MUST pass this exam with an 80%. So in the meantime please look at the SOP's, explore the site, and study basic aircraft Maneuvers. The exam will cover(Simulated Alaska Operations, Basic Aircraft Maneuvers, and Basic Flying Skills. Once you have pass this test you will then recceive another email saying you have been accepted.




      Sincerly,
      --------
      ";

            $from = "email";
            $headers = "From: $from";
            mail($to, $subject, $message, $headers);
            echo'<b>Application Sent ------- Staff</b>';
         } 
      } 
   } 
}

?>

 

Thanks

 

Link to comment
Share on other sites

Hi

 

Part of the issue is that we are now initialising $error.

 

count($error) is meant to bring back the number of members in an array. Here is is being used to check if $error has been set, and having initialised it to "" it has been set so count returns 1. As count is non 0, if($cerror =='0') evaluates to false and so no email is sent nor is the database uploaded.

 

It was a change to fix an error you had that caused us to suggest initialising $error. That it used to work and now didn't suggests possibly a php option has been changed.

 

Simple solution is to change the line if($cerror =='0') to if($error !="")

 

All the best

 

Keith

Link to comment
Share on other sites

I have edited this part of my script:

$cerror = count($error);

  if ($cerror > 1 )

  {

      echo $error;?> <FORM><INPUT TYPE="BUTTON" VALUE="Fix Errors" ONCLICK="history.go(-1)"></FORM> <?php

  }

  else

  {

     

and now I am not receiving no errors I click submit with no input field and it just gives me a white page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.