<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Upload photo 1</title>

<form method="post" action="upload.php" enctype="multipart/form-data">
                <p>
              Photo name: 
            </p>
            <input type="text" name="Name"/>
            <p>
              Category : --->
              Choose Between Animals, Cars , Landscapes , People
            </p>
                        <input type="text" name="Category"/>
            <p>
              Creator            </p>
                        <input type="text" name="Creator"/>

            <p>
             Upload your photo :            

         <input type="file" name="photo"> 
        <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Photo"/>
          </form>
<br/>
</center>

           </head>




<?php

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

//This gets all the other information from the form
$name=isset($_POST['name']) ? $_POST['name'] : '';
$category=isset($_POST['category']) ? $_POST['category'] : '';
$pic=isset($_POST['photo']) ? $_POST['photo'] : '';
$creator=isset($_POST['creator']) ? $_POST['creator'] : '';



// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("gallery") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO images (name,category,photo,creator)
VALUES ('$name', '$category', '$pic', '$creator'") ;

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

//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['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.";
}
?>

Hello guys..I need your help here. I am getting these 2 errors every time I open this form
Notice: Undefined index: photo in C:\xampp\htdocs\upload.php on line 37

Notice: Undefined index: photo in C:\xampp\htdocs\upload.php on line 56

Can anyone help me out what I'm doing wrong?? Thank you! :)

I believe you are missing a colon on line 36 -

C:\documents

Yeah I noticed that a minute after I posted the code, I changed but the problem hasn't solved..I suppose that I somehow, have to use the isset in _$Files too..

Member Avatar for diafol

Yep - you must use isset. In order to solve this, you can send the form to a file handler file - this is a better solution as it prevents resubmitting the form on page refresh - a problem in itself.

Can you please explain me how can I do this??Because I am new in php...

It also looks like you have a case mismatch.

Your post is 'Name' and your if(isset($_POST)) is 'name'

Same goes for the other POST variables. These are case sensitive, I believe.

Also, line 53 is missing a ) before the ;

And something else, if somebody can help..When i upload something, it must be added in my database automatically, right? I ask because , although i have these tables and fields in my database, it is not updating..

You are right ryantroop..I changed them already! :)

maybe repost your new code, please?

I just put an "image_" in front of every field( ex $image_name)..Do you want to repost it?

if you have made the changes that we have talked about, there should be some significant naming changes. If you are still getting the above error, then yes it would be helpful to have a new code to look at to see where you are having problems.

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Upload photo 1</title>

<form method="post" action="upload.php" enctype="multipart/form-data">
                <p>
              Photo name: 
            </p>
            <input type="text" name="Name"/>
            <p>
              Category : --->
              Choose Between Animals, Cars , Landscapes , People
            </p>
                        <input type="text" name="Category"/>
            <p>
              Creator            </p>
                        <input type="text" name="Creator"/>

            <p>
             Upload your photo :            

         <input type="file" name="photo">&nbsp;
        <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Photo"/>
          </form>
<br/>
</center>

           </head>




<?php

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

//This gets all the other information from the form
$photo_name=isset($_POST['name']) ? $_POST['name'] : '';
$photo_category=isset($_POST['category']) ? $_POST['category'] : '';
$pic=isset($_POST['photo']) ? $_POST['photo'] : '';
$photo_creator=isset($_POST['creator']) ? $_POST['creator'] : '';



// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("gallery") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO images (name,category,photo,creator)
VALUES ('$photo_name', '$photo_category', '$pic', '$photo_creator'");

//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['photo']['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 upload works fine..the only problem is that when I enter for the first time I get the message that I mentioned in the beginning..Probably something with the isset..

I still think that since line 9 is:

<input type='input' name='Name'> (notice the upper case 'Name')

Then on line 40, you use $_POST['name'] (notice the lower case)

Try changing that, as I believe these are case sensitive.

same error..As the upload works and the only problem is that I get this error message as I enter the page, I still believe that I must put something containing isset..the only problem is that i dont know what..

if it's still the same lines getting the error (37 and 56), then I feel it's line 37 causing the problem.

You can try changing it to

$target .= basename($_FILE['photo']['name'])

then try:
die("$target")

and see what you get (should be a blank screen with a path name). If the path name is not what you expect, then you need to fix that.

I made this change and I get an error...

I had the same problem with undefined index when I was working the login page. I added the isset in _$post it was solved.Do you know probably a way to add in there?

What error?

If you want to check if post is set, then you simply use

if(isset($_POST)) {
//do stuff

}

Not sure how that would really help, other than preventing people from accessing the script without posting something.

the undefined index error...

Probably because when I first enter the page it has no value I get this error message.When I upload an image and after it works properly..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.