Unexpected T_IF error doesn't make sense :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
suuunly
Forum Newbie
Posts: 5
Joined: Sun Jun 26, 2011 7:54 am
Location: Faroe Islands -> Klaksvík

Unexpected T_IF error doesn't make sense :(

Post by suuunly »

Hi, I am creating a very simple CMS, and I'm adding a function to give the user the ability to call the subjects anything , such as "Subject1" €Subject2* %&Subject3!?=#, e.t.c without breaking the code.
But when I execute the code it just returns an error that says:

PHP Parse error: syntax error, unexpected T_IF in - on line 9

Here is my code:

Code: Select all

function mysql_prep( $value ) {
		$magic_quotes_active = get_magic_quotes_gpc();
		$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e PHP >= v4.3.0
		
		if( $new_enough_php ) { // PHP v4.3.0 or higher
			// undo any magic quote effects so mysql_real_escape_string can do the work.
			if( $magic_quotes_active ) { $value = stripslashes($value); }
			$value = mysql_real_escape_string($value);
		
		} else { // before php v4.3.0
			// if magic quotes aren't already on then add slashes manually
			if( !$magic_quotes_active ) { $value = addslashes($value); }
			// if magic quotes are active, then slashes already exist
		}
		return $value;
	}
I have looked it through up and down, but simply can't find the error, so I was wondering if some of you guys could seek it out for me :D
Thanks a million in advance :D
------------------------------------------------------------------------------------
Don't know it this will help you, but the function is used by another document which contains this code:

Code: Select all

<?php require_once('include/dbConnection.php'); ?>
<?php require_once('include/functions.php'); ?>
<?php
	$menu_name = mysql_prep($_POST['menu_name']);
	$position = mysql_prep($_POST['position']);
	$visible = mysql_prep($_POST['visible']);
?>
<?php
	$query = "INSERT INTO subjects (
		menu_name, position, visible
		) VALUES (
			'{$menu_name}', {$position}, {$visible}
		)";
		
	$result = mysql_query($query, $connection);
	if($result) {
		// SUCCESS
		header("Location: content.php");
		exit;
	} else {
		// Display error message
		echo "<p>Subject creation failed</p>";
		echo "<p>" . mysql_error() . "</p>";
	}
?>
<?php
	mysql_close($connection);
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Unexpected T_IF error doesn't make sense :(

Post by social_experiment »

Code: Select all

   $query = "INSERT INTO subjects (menu_name, position, visible) VALUES ( '{$menu_name}', '{$position}', '{$visible}' )";
What happens if you add quotation marks around the other 2 values in the query ($position & $visible)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Unexpected T_IF error doesn't make sense :(

Post by McInfo »

All of the posted code appears to be syntactically correct. I even checked it with NetBeans. The problem must be with the code that was not posted.

Here is an example code that causes a similar error:

Code: Select all

<?php
echo "There is no semicolon at the end of this line -->"
if (false);
Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in [...].php on line 3
Notice that the message says the error was discovered on line 3 but the error occurred because there is no semicolon to end the echo statement on line 2.
Idri
Forum Newbie
Posts: 19
Joined: Sun May 29, 2011 9:21 am

Re: Unexpected T_IF error doesn't make sense :(

Post by Idri »

McInfo is right.

Anyway, PHP should tell you which file it's found the error in.
That, including where the error is (line 9) should help you enough to fix this.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Unexpected T_IF error doesn't make sense :(

Post by social_experiment »

Idri wrote:Anyway, PHP should tell you which file it's found the error in.
That, including where the error is (line 9) should help you enough to fix this.
It's possible that the error is somewhere above the given line aswell, in this case, prior to line 9 (See McInfo's example).
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
suuunly
Forum Newbie
Posts: 5
Joined: Sun Jun 26, 2011 7:54 am
Location: Faroe Islands -> Klaksvík

Re: Unexpected T_IF error doesn't make sense :(

Post by suuunly »

social_experiment: I tried that in a desperate attempt to make it work, even though I know that it wouldn't really make a difference because they are integers and not strings :S
------------------------------------------------------------------------------------
McInfo: Yeah, I thought so too, but I checked the code over and over again, retyping it and everything, but I keep getting this same error :/
------------------------------------------------------------------------------------
Idri: Yeah, I probably should have told you that the error is from the function.php file. But I have encountered errors like these before, where they came from outside the function itself. But this doesn't seem to be the case this time, unless I have overlooked something more than 20 times :P
---------------
I was wondering if it could be something with the:

Code: Select all

$magic_quotes_active = get_magic_quotes_gpc();
.....

Code: Select all

if( $magic_quotes_active )
Do you guys think it is a valid statement? I have my doubts with it :P
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Unexpected T_IF error doesn't make sense :(

Post by McInfo »

There is nothing wrong with the syntax of the code you have posted.

Try opening your project in an IDE like NetBeans or Eclipse PDT. It will identify syntax errors almost immediately and mark them with wavy red lines.

Another way to isolate the error is to comment out parts of your code until the error goes away. Or work the other way--comment out everything and uncomment until the error reappears.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Unexpected T_IF error doesn't make sense :(

Post by social_experiment »

suuunly wrote:I probably should have told you that the error is from the function.php file. But I have encountered errors like these before, where they came from outside the function itself. But this doesn't seem to be the case this time, unless I have overlooked something more than 20 times
Paste the code from function.php (unless the mysql_prep() function is from said page).
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
suuunly
Forum Newbie
Posts: 5
Joined: Sun Jun 26, 2011 7:54 am
Location: Faroe Islands -> Klaksvík

Re: Unexpected T_IF error doesn't make sense :(

Post by suuunly »

McInfo / social_experiment: Hi, I downloaded Netbeans IDE, and tried to open the functions.php file with it. And it turns out that the reason it didn't work, was because of the application I used before ( Coda ), because it had added invisible -+ signs around the area after the ")". Luckily Netbeans detected them right away, so I removed all these hidden signs and what do you know.... it WORKS!!! :D :D :D
So what I've learned from this, is that I don't recommend using Coda for php scripting. ;)

Anyway..... I would really like to thank you all for your help, and taking the time to helping me :D
so.... THANKS A MILLION :D :D :D :D
Post Reply