Error with else statement (syntax error, unexpected 'else' (T_ELSE))

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

This error message keeps popping out (syntax error, unexpected ‘else’ (T_ELSE)) the else that has error is the one i bolded, can anyone help? I’ve tried including if else statement but it doesnt help


<?php

mysql_connect("localhost","root","") or die(mysql_error());

mysql_select_db("travelstoredb") or die(mysql_error());

$file = $_FILES['image']['tmp_name'];

if (!isset($file))
	echo"Please select an image";
else
{ 
	$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
	$image_name = addslashes( $_FILES['image']['name']);
	$image_size = getimagesize($_FILES['image']['tmp_name']) ;
}
	if ($image_size==FALSE)
		echo "That's not an image";
	else
	{
		if (!$insert = mysql_query("INSERT INTO storeimage VALUES ('','$image_name','$image')"));
		echo "There is a problem with uploading the image.";
		
		**else**
		{
			$lastid = mysql_insert_id();
			echo "Image uploaded.<p /> Your image:<p /><img src=get.php?id=$lastid>";
		}
	}

	}
	



?>



Semicolon after if is not a good idea :slight_smile:

if (!$insert = mysql_query("INSERT INTO storeimage VALUES ('','$image_name','$image')") {
    echo "There is a problem with uploading the image.";
} else {
    $lastid = mysql_insert_id();
    echo "Image uploaded.<p /> Your image:<p /><img src=get.php?id=$lastid>";
}

Thanks it worked! However new errors surfaced,
now it says that
" Undefined index: image"
“Undefined variable: image_size”

file = _FILES[‘image’][‘tmp_name’];

if (!isset($file))
echo"Please select an image";
else
{
image = addslashes(file_get_contents(_FILES[‘image’][‘tmp_name’]));
image_name = addslashes( _FILES[‘image’][‘name’]);
image_size = getimagesize(_FILES[‘image’][‘tmp_name’]) ;
}
if ($image_size==FALSE)

Undefined index: image

Make sure that the file is submitted with the image name. That is the only possible problem I can think of.

Undefined variable: image_size

This might be related to the fact that $image_size is defined in the if. If condition of if is false it will not be defined and in this line:

if ($image_size==FALSE)

the error will occur.

The whole code dealing with the file should be executed only if !isset($file) is false.

ok… Ive get rid of that error and now they say that my files are empty.

file_get_contents(): Filename cannot be empty

getimagesize(): Filename cannot be empty

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.