Unexpected T_ELSE

Hi there :slight_smile:

I have this user creation form. I also have the login. Now I’m trying to implement a “Keycode system”, where the user needs a special keycode to actually create the user. All the keycodes are stored in a seperate table called “keys”, where the users are stored in a row called “brugere”.

So i need to check if the keycode that they enter is acutally found in the row “keys”.
I have tried to do this by inserting this:

[php]
$amount = mysql_result(mysql_query(“SELECT COUNT(*) FROM keys WHERE key = ‘$_POST[keycode]’”),0);
if($amount < 1)
{
[/php]

But i get this error:
Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\opretbruger.php on line 25

I should propably post my code here it is:

[php]

<?php include ("database.php"); if($_GET['do'] == "opretbruger") { $antal = mysql_result(mysql_query("SELECT COUNT(*) FROM brugere WHERE brugernavn = '$_POST[brugernavn]'"),0); if($antal < 1) { if(!empty($_POST['brugernavn']) && !empty($_POST['kodeord']) && !empty($_POST['email'])) { [b] // THIS IS WHAT I INSERTED[/b] $amount = mysql_result(mysql_query("SELECT COUNT(*) FROM keys WHERE key = '$_POST[keycode]'"),0); if($amount < 1) { $password = md5($_POST['kodeord']); mysql_query("INSERT INTO brugere (brugernavn,password,email,specialcode) VALUES ('$_POST[brugernavn]','$password','$_POST[email]','$_POST[keycode]')")or die(mysql_error()); print 'Succes'; } else { print "The keycode was invalid"; } else { print "Please fill out all fields"; } } else { print "The requested username is already in use - please choose another username"; } } else { ?>



Create a user

Fill out the fields below to create a user

Username:

Password:

Email:

Keycode:

<?php } ?> [/php]

I’m truly sorry if all this is messy but i really hope someone will help me.

Thanks have a good day! :slight_smile:

I think you have too many brackets.

[php]mysql_query(“INSERT INTO brugere (brugernavn,password,email,specialcode) VALUES (’$_POST[brugernavn]’,’$password’,’$_POST[email]’,’$_POST[keycode]’)”)or die(mysql_error());
print ‘Succes’;
}else{
print “The keycode was invalid”;
}else{
print “Please fill out all fields”;
}else{
print “The requested username is already in use - please choose another username”;
}else{[/php]

I like to include my brackets and else’s on the same line so I know all brackets are accounted for.

to me it look like you are missing one closing bracket and in this section there is an error in the way it is coded:
[php]
if($amount < 1)
{

$password = md5($_POST[‘kodeord’]);

mysql_query(“INSERT INTO brugere (brugernavn,password,email,specialcode) VALUES (’$_POST[brugernavn]’,’$password’,’$_POST[email]’,’$_POST[keycode]’)”)or die(mysql_error());
print ‘Succes’;
}
else {
print “The keycode was invalid”;
}
else
{
print “Please fill out all fields”;
}[/php]

you have to remember an else statement is final! if you say if "bob = bob do this, else do this " you are saying if bob = ANYTHING else do this, that means it is final, you cannot add an else statement to an else statement, it will confuse the hell out of php because it does not know which else statement to use.

if you download notepadd++ then put the script in there then select the “language”, set it to php, then if you put the cursor next to a bracket/brace, it will highlight the closing bracket/brace for that statement. this will help you to see where your if statements are ending and the else statements are beginning!
once I did that then I could see that your missing bracket was here:
[php]
else {
print “The keycode was invalid”;
}
[/php]

it should be :

[php]else {
print “The keycode was invalid”;
}
}[/php]

that was a good tip except for when you are having if statements inside of if statements then sometimes you have to do things like if{if{if{if{}else{}}}} then it gets harder to keep up with and that is where a good text editor or php editor will help you with highlighting brackets and such. I know you use one lotHop which one do you prefer?

Dreamweaver.

With if’s within if’s I tab them out so they are in line with each other.

[php]if(){
if(){
}else{
}
}else{
Error
}[/php]

Hi there,

Just thought I would join in here as I find code formatting of paramount importance when coding. My personal preference is similar to lothop, but (personally) even clearer:
[php]if()
{
if()
{
//something
}
else
{
//something else
}
}
else
{
Error
}[/php]

It takes more lines, but will not affect PHP - it will however, make it much easier to scan over quickly when coming back to it at a later date. For the record, I personally use the IDE “Netbeans”.

Sponsor our Newsletter | Privacy Policy | Terms of Service