if else else else

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
doug76
Forum Commoner
Posts: 26
Joined: Tue Aug 24, 2010 7:44 am

if else else else

Post by doug76 »

Hi,

I hope someone can help

I'm sure this is easy. My program works fine if I have only some else comments but when I ass another one, the program crashes. Is the answer to use switch? Even though the new comment is the only extra one I need? How do I add the extra condition? In this case it is the "Please enter the verification pass phrase exactly as shown".

I don't think there is anything inherently wrong with the code. I may well be wrong!

Any help much appreciated

code below

Code: Select all

 
<?php
session_start();
//Define the uplaod path and maximum file size constants
require_once('appvars.php');
require_once('connectvars.php');

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

// Grab the score data from the POST
	$first_name = trim($_POST['first_name']);
	$last_name = trim($_POST['last_name']);
	$score = trim($_POST['score']);
	$screenshot = trim($_FILES['screenshot']['name']);

	$user_pass_phrase = sha1($_POST['verify']);
	if ($_SESSION['pass_phrase'] == $user_pass_phrase) {

	if (!empty($first_name) && !empty($score) && !empty($screenshot)) {
	
	if ($_FILES['screenshot']['error'] == 0) {
//Move the file to the target upload folder
	$target = GW_UPLOADPATH . $screenshot;
	if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {

//Connect to the database
	$dbc = mysqli_connect('localhost', 'root', 'skipper', 'elvis_store');

//Write the data to the database
	$query = "INSERT into gwdb values (0, '$first_name', '$last_name', '$score', '$screenshot', 0)";
	mysqli_query($dbc, $query);
 
//Confirm success with the user
echo '<p>Thanks for submitting your new high score!</p>';
echo '<p><strong>First_Name:</strong> ' . $first_name . '<br />';
echo '<strong>Last_Name:</strong> ' . $last_name . ' <br />';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><a href="index1.php"><<Back to high scores</a></p>';

//Clear the score data to clear form
$first_name = "";
$last_name = "";
$score = "";
$screenshot ="";

mysqli_close($dbc);

}

else  {

echo '<p class="error">Sorry there was a problem uploading your screen shot image.</p>';
}
}
}
else {
	echo '<p class="error">The screen shot must be a GIF,JPEG or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';
}
//Try to delete the temporary screen shot image file
@unlink($_FILES['screenshot']['tmp_name']);
}
else

{

echo '<p class="error">Please enter all of the information to add your high score.</p>';
}

else {
echo '<p class="error">Please enter the verifaction pass phrase exactly as shown.</p>';
}}

?>  
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: if else else else

Post by requinix »

And how, exactly, is PHP supposed to know which of the elses it's supposed to execute?

if
jimboidaho
Forum Newbie
Posts: 8
Joined: Wed Jul 07, 2010 11:33 am

Re: if else else else

Post by jimboidaho »

PHP executes code from top to bottom. It will process the ifs and else one at a time from top to bottom. Your code has one } in the wrong place towards the bottom.

Code: Select all

}
else 
{
    echo '<p class="error">Please enter the verifaction pass phrase exactly as shown.</p>';
}
?>
doug76
Forum Commoner
Posts: 26
Joined: Tue Aug 24, 2010 7:44 am

Re: if else else else

Post by doug76 »

Thanks, but with this that I get this error:

Parse error: syntax error, unexpected T_ELSE

must be the } (I thoiught I had an extra one so deleted, made no diffrenece)

any ideas?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: if else else else

Post by McInfo »

The problem wasn't that that there was an extra curly bracket. As jimboidaho said, a curly bracket was in the wrong place. The one on line 55 should have been moved to line 68.

Code: Select all

echo '<p class="error">Sorry there was a problem uploading your screen shot image.</p>';
}
}
} // Line 55

Code: Select all

echo '<p class="error">Please enter all of the information to add your high score.</p>';
}
// Line 68
Using consistent indentation really helps to clarify code structure. Also, if your editor does not point out syntax errors to you, I suggest you upgrade to one that does.

Check your error messages for spelling and punctuation errors.
doug76
Forum Commoner
Posts: 26
Joined: Tue Aug 24, 2010 7:44 am

Re: if else else else

Post by doug76 »

Thank you very much!

I am still learning and have been using simple notepad. I think an editor would be beneficial.
Post Reply