Jump to content

unexpected T_ELSE


bravo14

Recommended Posts

Hi Guys

 

I have the following error

 

Parse error: syntax error, unexpected T_ELSE in /home/sites/maypolejuniors.com/public_html/subscribe.php on line 168

from the following code

<?php
include_once('includes/connect.php');
//assign form entries from subscription form to variables
$email=$_POST["form_email"];
$name=$_POST["form_name"];
$status=$_POST["form_subscribe2"];
echo ("status ".$status);
	if($status=="1"){
			$result="$sql = 'SELECT * FROM `email_table` where `email`=$email";
			if(mysql_num_rows($result) > 0) 
					{ 
   						echo('The email address '.$email.' is already subscribed to the Maypole Juniors newsletter'); 
					} 
	else 
		{ 
			$sql="INSERT INTO `email_table` (`name`,`email`)	VALUES ('".$name."', '".$email."')";

			if (!mysql_query($sql,$con)){

				die('Error: ' . mysql_error());
				}
			else
				{
			echo($name. ' who has '.$email.' as their email address has successfully subscribed to the Maypole Juniors newsletter');
			}
		}
		else  //line 168
		{
		echo("Do something else");
		$sql="DELETE FROM email_table WHERE email='".$email."'";
		if (!mysql_query($sql,$con)){

				die('Error: ' . mysql_error());
				}
				else
				{
			echo($name. ' who has '.$email.' as their email address has successfully unsubscribed from the Maypole Juniors newsletter');
		}
		}
	}
mysql_close($con);
?>

 

Any ideas what I have wrong?

 

Link to comment
Share on other sites

if($status=="1"){
            $result="$sql = 'SELECT * FROM `email_table` where `email`=$email";
            if(mysql_num_rows($result) > 0) 
                  { 
                     echo('The email address '.$email.' is already subscribed to the Maypole Juniors newsletter'); 
                  } 
} // missing bracket      
else 
         { 

 

You are missing a bracket

Link to comment
Share on other sites

Thanls for that, I now have the same on line 171

 

<?php
include_once('includes/connect.php');
//assign form entries from subscription form to variables
$email=$_POST["form_email"];
$name=$_POST["form_name"];
$status=$_POST["form_subscribe2"];
echo ("status ".$status);
	if($status=="1"){
			$result="$sql = 'SELECT * FROM `email_table` where `email`=$email";
//is the email address already subsribed
			if(mysql_num_rows($result) > 0) 
					{ 
					//The email address already exists in the table, output message
   						echo('The email address '.$email.' is already subscribed to the Maypole Juniors newsletter'); 
					} 
	}
	else //if not then enter into database
		{ 
			$sql="INSERT INTO `email_table` (`name`,`email`)	VALUES ('".$name."', '".$email."')";

			if (!mysql_query($sql,$con)){

				die('Error: ' . mysql_error());
				}
						else
				{
			echo($name. ' who has '.$email.' as their email address has successfully subscribed to the Maypole Juniors newsletter');
			}
		}
		else //line 171
		{
		echo("Do something else");
		$sql="DELETE FROM email_table WHERE email='".$email."'";
		if (!mysql_query($sql,$con)){

				die('Error: ' . mysql_error());
				}
				else
				{
			echo($name. ' who has '.$email.' as their email address has successfully unsubscribed from the Maypole Juniors newsletter');
		}
		}

mysql_close($con);
?>

Link to comment
Share on other sites

Exactly. Try this (its what I use, though it's not the standard):

 

if(something)
{
  if(something else)
  {
     // do whatever
  }
  else
  {
    // whatever
  }
}
else
{
  if(another thing)
  {
    // do whatever
  }
  else
  {
    //do whatever
  }
}

 

See how easy that is to read? And if there is a brace missing, you can figure out where it is very fast. You can also figure out where the problem is when if/else statements are acting unexpectedly.

Link to comment
Share on other sites

Cheers Guys

 

I have got the T Else to disappear and the Insert query works but I get the following errors displayed

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sites/maypolejuniors.com/public_html/subscribe2.php on line 153

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/sites/maypolejuniors.com/public_html/subscribe2.php on line 158

 

The code now lookds like this

 


include_once ('includes/connect.php');
//assign form entries from subscription form to variables 
$email = $_POST["form_email"]; 
$name = $_POST["form_name"]; 
$status = $_POST["form_subscribe2"]; 
echo ("status " . $status); 
$result="$sql='SELECT *FROM `email_table` where `email`=$email";

if(mysql_num_rows($result)>0)
{
echo('The email address '.$email.' is already subscribed to the Maypole Juniors newsletter');
}
else
if(mysql_numrows($result)==0)
{
	$sql="INSERT INTO `email_table` (`name`,`email`) VALUES ('".$name."','".$email."')";
	if(!mysql_query($sql,$con))
	{
		die('Error: '.mysql_error());
	}
	else
		{
			echo($name.' who has '.$email.' as their email address has successfully subscribed to the Mayple Juniors newsletter');
		}
}
else
{
	$sql="DELETE FROM `email_table` WHERE email='".$email."'";
	if(!mysql_query($sql,$con))
	{
		die('Error: '.mysql_error());
	}
	else
		{
			echo($name.' who has '.$email.' as their email address has successfully unsubscribed from the Maypole Juniors newsletter');
		}
}

?>

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.