Hi all,

Here is all my code for processing a form. Having trouble with the last else statement. The error is saying it is not expecting that last else!!
Sorry for all the code, but I thought I would put it all in here.
Could somebody please let me know where I have gone wrong, but importantly also why?

<?php 
session_start();
//process_new_member.php
if(($_SESSION['id']) && ($_SESSION['access_level'] == 1))
{
?>
<html>
<head>
    <title>Highlands 4x4 Response Group - Process New Member</title>
    <meta name="title" content="Highlands 4x4 Response Group">
    <meta name="generator" content="Microsoft FrontPage 6.0">
    <link rel="shortcut icon" href="images/favicon.ico">
    <meta http-equiv="Window-target" content="_top">
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
    <meta http-equiv="Content-Language" content="en-gb">
    <meta name="MSSmartTagsPreventParsing" content="TRUE"><!--A voluntary group of 4x4 enthusiasts providing a service to statutory and other bodies in times of crisis and severe weather-->
    <meta name="description" content="A voluntary group of 4x4 enthusiasts providing a service to statutory and other bodies in times of crisis and severe weather">
    <meta name="Abstract" content="A voluntary group of 4x4 enthusiasts providing a service to statutory and other bodies in times of crisis and severe weather">
    <meta name="keywords" content="new member,highland,4x4,response">
    <meta name="Robots" content="index, follow">
    <meta name="Distribution" content="Global">
    <meta name="Revisit-After" content="30 days">
    <meta name="Rating" content="General">
    <meta name="Reply-to" content="carterlangley@btinternet.com">
    <meta name="Owner" content="Highland 4x4 Response Group">
    <meta name="Author" content="Carter Langley">
    <meta name="copyright" content="Carter Langley - 2010">
    <meta name="generator" content="Microsoft FrontPage 6.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <link rel="stylesheet" type="text/css" href="mycss.css">
    <link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
  <div align="center">
    <table border="0" width="100%" id="table1" cellspacing="0" cellpadding="0" bgcolor="#000000">
      <tr>
        <td width="10%" height="0%" align="left">
            <a href="" target="_blank">
            <img border="0" src="images/logo_image.jpg" width="136" height="136" alt="Highland 4x4 Response Group"></a>
        </td>

        <td width="68%" align="center">
        <span class="mainbannertext">highland 4x4 response group</span>
        <table border="0" style="border-collapse: collapse" width="100%" cellpadding="0">
            <tr>
                <td valign="middle">
                <p align="center">
                <span class="subbannertext">registered charity - sc041948</span><br>
                <span class="subbannertext">click
                <a title="OSCR Listing for Highland 4x4 Response Group" target="_blank" href="">here</a> for the oscr listing</span></td>
            </tr>
        </table>
        </td>

        <td width="22%">
          <p align="right">
            <a href="http://www.4x4response.info/" target="_blank">
            <img border="0" src="images/national4x4logo.gif" width="288" height="74" alt="The National 4x4 Response Network"></a></p>
        </td>
      </tr>
    </table>
  </div>
  <br>
  <table border="1" width="100%" cellpadding="0" style="border-collapse: collapse">
    <tr>
        <td align="left" width="34%" height="100" bgcolor="#808080">
        <p class="subbannertext ">your banner advert can be here</p></td>
        <td align="center" width="33%">
        <div align="center">
            <p class="pageheadertext">Process New Member</p></div></td>
        <td align="right" width="33%" bgcolor="#808080">
        <p class="subbannertext ">your banner advert can be here</p></td>
    </tr>
    </table><br>

<form method="post" action="process_new_member.php">
    <div align="center">
        <table class="hovertable" border="0" style="border-collapse: collapse" width="100%" cellspacing="2" cellpadding="2">
            <tr>
                <td valign="top" colspan="6">
                    <p class="headingtext">Personal Information</p>
                </td>
            </tr>
            <tr>
                <td valign="top">first name</td>
                <td valign="top"><input type="text" name="first_name" size="20"></td>
                <td valign="top">middle name</td>
                <td valign="top"><input type="text" name="middle_name" size="20"></td>
                <td valign="top">last name</td>
                <td valign="top"><input type="text" name="last_name" size="20"></td>
            </tr>
            <tr>
                <td valign="top">group id (hi number)</td>
                <td valign="top"><input type="text" name="group_id" size="20"></td>
                <td valign="top">date of birth</td>
                <td valign="top"><input type="text" name="dob" size="20"></td>
                <td valign="top">NI number</td>
                <td valign="top"><input type="text" name="ni_number" size="20"></td>
            </tr>
            <tr>
                <td valign="top">home telephone number</td>
                <td valign="top"><input type="text" name="home_tel" size="20"></td>
                <td valign="top">personal mobile number</td>
                <td valign="top"><input type="text" name="home_mobile" size="20"></td>
                <td valign="top">personal email address</td>
                <td valign="top"><input type="text" name="home_email" size="20"></td>
            </tr>
            <tr>
                <td valign="top">home address</td>
                <td valign="top" colspan="2">
                <textarea rows="5" name="home_address" cols="40"></textarea></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">city/town</td>
                <td valign="top"><input type="text" name="home_town" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">county</td>
                <td valign="top"><input type="text" name="home_county" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">postal code</td>
                <td valign="top"><input type="text" name="home_postal_code" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top" colspan="6">
                    &nbsp;</td>
            </tr>
            <tr>
                <td valign="top" colspan="6">
                    <p class="headingtext">employment information</p>
                </td>
            </tr>
            <tr>
                <td valign="top">company</td>
                <td valign="top"><input type="text" name="company" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">work telephone number</td>
                <td valign="top"><input type="text" name="work_tel" size="20"></td>
                <td valign="top">work mobile number</td>
                <td valign="top"><input type="text" name="work_mobile" size="20"></td>
                <td valign="top">work email address</td>
                <td valign="top"><input type="text" name="work_email" size="20"></td>
            </tr>
            <tr>
                <td valign="top">work address</td>
                <td valign="top" colspan="2">
                <textarea rows="5" name="work_address" cols="40"></textarea></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">city/town</td>
                <td valign="top"><input type="text" name="work_town" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">county</td>
                <td valign="top"><input type="text" name="work_county" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">postal code</td>
                <td valign="top"><input type="text" name="work_postal_code" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top" colspan="6">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top" colspan="6">
                    <p class="headingtext">emergency contact information</p>
                </td>
            </tr>
            <tr>
                <td valign="top">name</td>
                <td valign="top"><input type="text" name="emergency_name" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">telephone number</td>
                <td valign="top"><input type="text" name="emergency_tel" size="20"></td>
                <td valign="top">alternative telephone number</td>
                <td valign="top"><input type="text" name="emergency_alt_tel" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">address</td>
                <td valign="top" colspan="2">
                <textarea rows="5" name="emergency_address" cols="40"></textarea></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">city/town</td>
                <td valign="top"><input type="text" name="emergency_town" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">county</td>
                <td valign="top"><input type="text" name="emergency_county" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">postal code</td>
                <td valign="top"><input type="text" name="emergency_postal_code" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top" colspan="6">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top" colspan="6">
                    <p class="headingtext">spouse information</p>
                </td>
            </tr>
            <tr>
                <td valign="top">name</td>
                <td valign="top">
                <input type="spouse_name" name="spouse_name" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">telephone number home</td>
                <td valign="top"><input type="text" name="spouse_home_tel" size="20"></td>
                <td valign="top">telephone number work</td>
                <td valign="top"><input type="text" name="spouse_work_tel" size="20"></td>
                <td valign="top">mobile telephone number</td>
                <td valign="top"><input type="text" name="spouse_mobile" size="20"></td>
            </tr>
            <tr>
                <td valign="top">home email address</td>
                <td valign="top"><input type="text" name="spouse_home_email" size="20"></td>
                <td valign="top">work email address</td>
                <td valign="top"><input type="text" name="spouse_work_email" size="20"></td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top" colspan="6">
                <input type="submit" value="Submit" name="submit">&nbsp;&nbsp;<input type="reset" value="Reset" name="reset"></td>
            </tr>
            </table>
    </div>
</form>
<?php
}
else
{
include "db_connect.php";
$first = protect($_POST['first_name']);
$first = protect($_POST['middle_name']);
$first = protect($_POST['last_name']);
$first = protect($_POST['group_id']);
$first = protect($_POST['dob']);
$first = protect($_POST['ni_number']);
$first = protect($_POST['home_tel']);
$first = protect($_POST['home_mobile']);
$first = protect($_POST['home_email']);
$first = protect($_POST['home_address']);
$first = protect($_POST['home_town']);
$first = protect($_POST['home_county']);
$first = protect($_POST['home_postal_code']);
$first = protect($_POST['company']);
$first = protect($_POST['work_tel']);
$first = protect($_POST['work_mobile']);
$first = protect($_POST['work_email']);
$first = protect($_POST['work_address']);
$first = protect($_POST['work_town']);
$first = protect($_POST['work_county']);
$first = protect($_POST['work_postal_code']);
$first = protect($_POST['emergency_name']);
$first = protect($_POST['emergency_tel']);
$first = protect($_POST['emergency_alt_tel']);
$first = protect($_POST['emergency_address']);
$first = protect($_POST['emergency_town']);
$first = protect($_POST['emergency_county']);
$first = protect($_POST['emergency_postal_code']);
$first = protect($_POST['spouse_name']);
$first = protect($_POST['spouse_home_tel']);
$first = protect($_POST['spouse_work_tel']);
$first = protect($_POST['spouse_mobile']);
$first = protect($_POST['spouse_home_email']);
$first = protect($_POST['spouse_work_email']);
$errors = array();
$regex = "/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+.[a-z]{2,}$/i";
    if(!preg_match($regex, $home_email))
    {
      $errors[] = "Home E-mail is not in name@domain format!";
    }
    if(!preg_match($regex, $work_email))
    {
      $errors[] = "Work E-mail is not in name@domain format!";
    }
    if(!preg_match($regex, $spouse_home_email))
    {
      $errors[] = "Spouse Home E-mail is not in name@domain format!";
    }
    if(!preg_match($regex, $spouse_work_email))
    {
      $errors[] = "Spouse Work E-mail is not in name@domain format!";
    }
    if(!$first_name || !$last_name || !$dob || !$home_postal_code)
    {
       $errors[] = "You did not fill out the required fields - First Name, Last Name, Date of Birth, Home Postal Code";
    }
    if(count($errors) > 0)
    {
      echo "The following errors occured with your registration";
      echo "<font color=\"red\">";
      foreach($errors AS $error)
      {
        echo $error . "\n";
        echo "<br>";
      }
      echo "</font>";
      echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
      //we use javascript to go back rather than reloading the page 
      // so the user doesn't have to type in all that info again.
    }

  $sql = "INSERT into `members`(`first_name`,`middle_name`,`last_name`,`group_id`,`dob`,`ni_number`,`home_tel`,`home_mobile`,`home_email`,`home_address`,`home_town`,`home_county`,`home_postal_code`,`company`,`work_tel`,`work_mobile`,`work_email`,`work_address`,`work_town`,`work_county`,`work_postal_code`,`emergency_name`,`emergency_tel`,`emergency_alt_tel`,`emergency_address`,`emergency_town`,`emergency_county`,`emergency_postal_code`,`spouse_name`,`spouse_home_tel`,`spouse_work_tel`,`spouse_mobile`,`spouse_home_email`,`spouse_work_email`)
  VALUES ('$first_name','$middle_name','$last_name','$group_id','$dob','$ni_number','$home_tel','$home_mobile','$home_email','$home_address','$home_town','$home_county','$home_postal_code','$company','$work_tel','$work_mobile','$work_email','$work_address','$work_town','$work_county','$work_postal_code','$emergency_name','$emergency_tel','$emergency_alt_tel','$emergency_address','$emergency_town','$emergency_county','$emergency_postal_code','$spouse_name','$spouse_home_tel','$spouse_work_tel','$spouse_mobile','$spouse_home_email','$spouse_work_email')";

 $query = mysql_query($sql) or die(mysql_error());
}

else
{
    echo "<script type=\"text/javascript\">;
    alert(\"You don't belong here!\");
    window.location=\"index.htm\"</script>";
}
}
?>

Dani AI

Generated

Good catch by — the duplicate else was the immediate parse error, and ’s suggestion to reflow the auth/logic fixed that. Since reports the form now displays and inserts, here are a few concise, practical improvements to avoid subtle bugs and tighten security.

Keep authentication and redirects before any HTML output so you can use server-side redirects safely. A minimal structure to follow is:

<?php
session_start();

// block unauthorised users before output
if (empty($_SESSION['id']) || $_SESSION['access_level'] !== 1) {
    header('Location: index.htm');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // validate & insert
} else {
    // show the HTML form
}
?>

Use one variable per form field (don’t overwrite a single variable repeatedly), validate server-side, and stop processing on errors. Example:

$first_name = trim($_POST['first_name'] ?? '');
$home_email = filter_var($_POST['home_email'] ?? '', FILTER_SANITIZE_EMAIL);

if (!filter_var($home_email, FILTER_VALIDATE_EMAIL)) {
    $errors[] = 'Invalid home email';
}

if (!empty($errors)) {
    // show errors and return; do NOT run the INSERT
    return;
}

Replace old mysql_* calls with prepared statements (PDO or mysqli) to prevent SQL injection and to get proper error handling. Example pattern:

$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('INSERT INTO members (first_name, last_name, home_email) VALUES (:first, :last, :email)');
$stmt->execute([':first'=>$first_name, ':last'=>$last_name, ':email'=>$home_email]);

Quick checklist: move auth check to top; use $_SERVER['REQUEST_METHOD'] === 'POST' to detect submissions; assign each $_POST to its own variable; validate with filter_var; bail out on validation errors before inserting; use prepared statements and exit after redirects. These steps will keep the script readable, secure, and easier to debug.

Recommended Answers

All 4 Replies

You have two else statements for one if statement which is syntactically incorrect:

if(($_SESSION['id']) && ($_SESSION['access_level'] == 1)) {

} else {

} else { // this second else on line 383 is incorrect

}

You can use elseif(another condition) if that is what you require.

Hi,

Where do want to put the last else? Your conditions are set like this

if(){

## this is your first if

}

else{

## this is else for the if above..

## you have all the ifs here for error checking
## below is your last if within this else

if(){


} ## end of if followed by your database query

#your query here

} ## end of else statement

## then you set another else statement on line 383

else{

}

## then you have your extra curly bracket below

} // line 389

You have little over one curly bracket on line 389. Now let us go back to my question. Where do want the last else to be included? You want it within the first else,? following the last if?

If so, then remove the entire else..

  else
   {
   echo "<script type=\"text/javascript\">;
   alert(\"You don't belong here!\");
   window.location=\"index.htm\"</script>";
    }

and put it just below line 376 and above your database query.

remove the last curly bracket on line 389.

test your script..

Hi veedeo,

Thanks for this. Now the people with the right access level can access the form. Only thing remaining now is - If a person with the wrong access level tries to access this page, the error scripts run instead of them being sent back to the index.htm page. Is this just a case of moving that block of code to above the error scripts?

Ok, all sorted out now. Only the right access level can get to the form now and then it is displaying properly and the data is sent to the database with no problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.