0
<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! :)

Edited by Ulfang

3
Contributors
19
Replies
20
Views
4 Years
Discussion Span
Last Post by Ulfang
Featured Replies
  • 1
    diafol 3,669   4 Years Ago

    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. Read More

0

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..

1

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.

0

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

0

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 ;

Edited by ryantroop

0

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..

0

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

0

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

0

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.

0
<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..

0

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.

0

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..

0

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.

0

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?

0

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.

0

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..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.