-4

I tried to upload a file into a mySQL database using this code

 <html>
    <body>

    <label for="exampleInputPassword1">Upload a photograph</label>
        <input type="file" class="form-control" id="file" tname="file"  required>

      <button type="submit" class="btn btn-default">Submit</button>
    </body>
</html>

<?php
if ($_FILES["file"]["error"] > 0) {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?> 

But I get these notices

Notice: Undefined index: file in C:\xampp\htdocs\tachi\html\admin\addplace\insert_place.php on line 2

Notice: Undefined index: file in C:\xampp\htdocs\tachi\html\admin\addplace\insert_place.php on line 5
Upload:

Notice: Undefined index: file in C:\xampp\htdocs\tachi\html\admin\addplace\insert_place.php on line 6
Type:

Notice: Undefined index: file in C:\xampp\htdocs\tachi\html\admin\addplace\insert_place.php on line 7
Size: 0 kB

Notice: Undefined index: file in C:\xampp\htdocs\tachi\html\admin\addplace\insert_place.php on line 8
Stored in: 

How to fix this problem. From where can I identify whether the photograph has been uploaded into the database and how?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Chinthani
  • 45
  • 1
  • 1
  • 7

1 Answers1

2

You don't have a valid form. For starters, you've completely omitted the <form> tag. Without it, you can't add the enctype attribute required for uploads.

<form action="insert_place.php" method="POST" enctype="multipart/form-data">

Also, you used an invalid attribute name for your upload field. There is no tname attribute. It's just name:

<input type="file" class="form-control" id="file" name="file"  required>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    Seeing that OP's *most* probably using the entire code inside the same page, may still get and `Undefined index...` since is not wrapping PHP inside a conditional statement. Using `action=""` may be an option along with an `isset()` and/or `empty()` conditional. Just lookin' at every possible *angle*. ;) – Funk Forty Niner Sep 08 '14 at 20:05
  • @Fred-ii- There is a lot of room for improvement in this code. Hopefully this points them in the right direction. Hopefully. – John Conde Sep 08 '14 at 20:16
  • I'm sure it will John. – Funk Forty Niner Sep 08 '14 at 20:19
  • Thank you all finally I could fix it with empty(). I made some changes to the code again and now I get another error.It is "Error: Unknown column 'photo.jpg' in 'field list'" – Chinthani Sep 09 '14 at 13:05
  • @Belinda2 That's a whole different question. You should ask that question separately showing the relevant code for that. – John Conde Sep 09 '14 at 13:07