954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Undefined index error

Hello.

I'm getting an undefined index error with the following code and I can't for the life of me figure out why. Any insight would be greatly appreciated.

I'm using an html form and a PHP file to upload images to a directory and write the rest of the form details to my DB. The images get uploaded to the directory just fine, but it's not writing the rest of the data to the DB for some reason.

.html form

<html>
<form enctype="multipart/form-data" action="add.php" method="POST"> 
Name: <input type="text" name="name"> 
E-mail: <input type="text" name = "email"> 
Phone: <input type="text" name = "phone"> 
Photo: <input type="file" name="photo"> 
<input type="submit" value="Add"> 
</form>
</html>


and here is the add.php file for uploading and writing to the database

<?php 
//This is the directory where images will be saved 
$target = 'images/'; 
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=($_FILES['photo']['name']); 

// Connects to your Database 
mysql_connect("localhost", "un", "pw") or die(mysql_error()) ; 
mysql_select_db("database") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `products` VALUES ('$name', '$email', '$phone', '$pic')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?>


the undefined index error I'm getting is:
"Undefined index: uploadedfile in /Applications/MAMP/htdocs/upload/add.php on line 24"

then below that I get the upload confirmation message that reads;
"The file has been uploaded, and your information has been added to the directory"


thanks a lot in advance!

dottomm
Junior Poster in Training
89 posts since Nov 2007
Reputation Points: 9
Solved Threads: 5
 
$_FILES['uploadedfile']['name']


Shouldn't this be $_FILES['photo']['name'] :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

DOH!
Thank you!! Thank you! It works now!

dottomm
Junior Poster in Training
89 posts since Nov 2007
Reputation Points: 9
Solved Threads: 5
 

:) Great !

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hey man!

i got a question maybe you can help me with this....

the HTML CODE: (inserir_produto.php)

<style>

span {width: 150;}

</style>

  <form action='inserir_produto1.php' method="post">
	<h3><b>Inserir Produto</b></h3>
    <span>Marca: </span><input type='text' name='marca' />
    
    <span>Cor Principal: </span><input type='text' name='cor' />
    
    <span>Material de Fabrico: </span><input type='text' name='matfabrico' />
    
    <span>Altura: </span><input type='text' name='altura' />
    
    <span>Largura: </span><input type='text' name='largura' />
    
    <span>Referência: </span><input type='text' name='ref' />
    
    <span>Foto: </span><input type="file" name="file" />
    
    <span>Descrição: </span><input type='text' name='descricao' />
    
    <input type='submit' name='submit1' value='Adicionar Produto' id='submit1' />
    <input type="reset" value="Limpar Tudo" />
    
	</form>

AND THIS IS THE PHP CODE: (inserir_produto.php)

<?php 



if (isset($_POST['submit1']))
	if(	$_POST['marca']!=''			&&
		$_POST['cor']!=''			&&
		$_POST['matfabrico']!=''	&&
		$_POST['altura']!=''		&&
		$_POST['largura']!=''		&&
		$_POST['ref']!=''			&&
		$_POST['file']!=''			&&
		$_POST['descricao']!='')
		
		{
	mysql_connect('localhost','root','');

	mysql_select_db('albino');
	
	
	$sql2="SELECT * from produtos WHERE referencia ='" . $_POST['ref'] . "'";
	$resultado=mysql_query($sql2);
				mysql_fetch_array($resultado);
		
				if (mysql_affected_rows()==0)
				{
					$ficheiro=$_FILES['foto'];
					$nome_final=$_POST['ref'] . "_foto.jpg";
				
					if ($ficheiro['error']==0)
					{
						if ( 
			    			in_array( $ficheiro['type'], array ( 'image/pjpeg', 'image/jpeg', 'image/png','image/gif' ) ) 
							)
						{
							@move_uploaded_file ($ficheiro['tmp_name'], 'foto/' . $nome_final);
							mysql_connect('localhost','root','');
							mysql_select_db('albino');
	}
	}}
	
	
	
	
	$sql="insert into produtos(marca, cor_principal, mat_fabrico, altura, largura, referencia, foto, descricao) values ('" .  $_POST['marca']  .  "','"  .  $_POST['cor']  .  "','"  .  $_POST['matfabrico']  .  "','" . $_POST['altura'] . "','" . $_POST['largura'] . "','" . $_POST['ref'] . "','" . $_POST['file'] . "','" . $_POST['descricao'] . "')";
	
	mysql_query($sql);
	echo "Produto inserido com sucesso! <a href='inserir_produto.php'>Voltar</a>";
	}
	
	else {
	echo "Erro! Verifique o formulário!<a href='inserir_produto.php'>Voltar</a>";
	}


?>

THE THING IS THAT THE IMAGE THAT I WANT TO UPLOAD IS NOT UPLOADING TO THE SPECIFIC FOLDER...THE ERROR THAT APPEARS ME IS THIS ONE:

Notice: Undefined index: foto in C:\wamp\www\A.S.S\inserir_produto1.php on line 28


CAN YOU HELP ME? IM FREAKING OUT WITH THIS...

I WOULD BE VERY APPRECIATED....

PS.: SORRY ABOUT MY ENGLISH, IM PORTUGUESE

tiago070
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

:) As the error itself says, foto is undefined.

$ficheiro=$_FILES['foto'];

should be

$ficheiro=$_FILES['file'];

One more thing, notices are usually generated when you don't initialize a variable [and start using it]. Check out php.net -> error_reporting for more details.
Cheers,
Naveen

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

sir/mam,

I'm getting an undefined index error with the following code and I can't for the life of me figure out why. Any insight would be greatly appreciated.

I'm using an html form and a PHP file to upload images to a directory and write the rest of the form details to my DB. The images get uploaded to the directory just fine, but it's not writing the rest of the data to the DB for some reason.

<?php

$hotelname=$_REQUEST['hotelname'];
$roomtype=$_REQUEST['roomtype'];
$roomcategory=$_REQUEST['roomcategory'];
$inclusion=$_REQUEST['inclusion'];
$price=$_REQUEST['price'];
$roomdesc=$_REQUEST['roomdesc'];
$amenities=$_REQUEST['amenities'];

echo $photo=($_FILES['photo']['name']);

$status=$_REQUEST['status'];

include_once('conn.php');

$sql=" Insert into addhotel set
Hotel_name='$hotelname',
Room_type='$roomtype',
Room_category='$roomcategory',
Inclusion='$inclusion',
Charges='$price',

Rooms_description='$roomdesc',
Hotel_amenities='$amenities',
Hotel_image='$photo',
Status='$status'";


$result=mysql_query($sql) or die ('Query Not Executed '.mysql_error());
if ($result)
{
//pic();

if (pic()==true)
{
echo "profile updated............";

}

}
else
{
echo "profile not updated............";
}


function pic()
{
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/pjpeg"))
&& ($_FILES["photo"]["size"] > 20000)){

if (move_uploaded_file($_FILES["photo"]["tmp_name"],
"admin/upload/" . $_FILES["photo"]["name"]))
{
return true;
exit();
}
else
{
return false;
exit();
}
}
else
{
return false;
exit();
}
}


mysql_close() ;

?>


thanks and regards brijeshwart tiwari

brijeshwartiwar
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

sir/mam,

I'm getting an undefined index error with the following code and I can't for the life of me figure out why. Any insight would be greatly appreciated.

I'm using an html form and a PHP file to upload images to a directory and write the rest of the form details to my DB. The images get uploaded to the directory just fine, but it's not writing the rest of the data to the DB for some reason.

<?php

$hotelname=$_REQUEST['hotelname'];
$roomtype=$_REQUEST['roomtype'];
$roomcategory=$_REQUEST['roomcategory'];
$inclusion=$_REQUEST['inclusion'];
$price=$_REQUEST['price'];
$roomdesc=$_REQUEST['roomdesc'];
$amenities=$_REQUEST['amenities'];

echo $photo=($_FILES['photo']['name']);

$status=$_REQUEST['status'];


include_once('conn.php');

$sql=" Insert into addhotel set
Hotel_name='$hotelname',
Room_type='$roomtype',
Room_category='$roomcategory',
Inclusion='$inclusion',
Charges='$price',

Rooms_description='$roomdesc',
Hotel_amenities='$amenities',
Hotel_image='$photo',
Status='$status'";

$result=mysql_query($sql) or die ('Query Not Executed '.mysql_error());
if ($result)
{
//pic();

if (pic()==true)
{
echo "profile updated............";

}

}
else
{
echo "profile not updated............";
}


function pic()
{
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/pjpeg"))
&& ($_FILES["photo"]["size"] > 20000)){

if (move_uploaded_file($_FILES["photo"]["tmp_name"],
"admin/upload/" . $_FILES["photo"]["name"]))
{
return true;
exit();
}
else
{
return false;
exit();
}
}
else
{
return false;
exit();
}
}


mysql_close() ;

?>

geting error

Notice: Undefined index: photo in D:\wamp\www\budgethotelsdelhi.net\admin\addhotels.php on line 14

Notice: Undefined index: photo in D:\wamp\www\budgethotelsdelhi.net\admin\addhotels.php on line 57

Notice: Undefined index: photo in D:\wamp\www\budgethotelsdelhi.net\admin\addhotels.php on line 58

Notice: Undefined index: photo in D:\wamp\www\budgethotelsdelhi.net\admin\addhotels.php on line 59

brijeshwartiwar
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

hi I am student and trying to upload an image in the data base
I am getting an error


Notice: Undefined index: image in C:\xampp\www\display_img\TMP49d65v5y5t.php on line 17


I am using XAMPP and MacroMedia DreamWeaver 8

and my code is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>upload an image</title>
</head>

<body>
	
<form action="index.php" method="post" enctype="multipart/form-data">
	File:
	<input type="file" name="image"  /> <input type="submit" value="upload" />
</form>
<?php
	mysql_connect("localhost","root","") or die(mysql_error());
	mysql_select_db("databaseimage") or die(mysql_error());
	$file = $_FILES["image"]["tmp_name"];
?>
</body>
</html>
roshu4u46
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You