I have built a form whereby i use it to upload image files which is then to be stored on my server and it's information e.g description of image, to be saved on my mysql db. I am having trouble with php not recognisiing my 'photo' variable as it is giving me the following error when i submit my form:
Notice: Undefined index: photo in C:\\wamp\\www\\Shanghai2010\\index.php on line 50
Notice: Undefined index: photo in C:\\wamp\\www\\Shanghai2010\\index.php on line 51
along with my user generate error:
Database error storing file information
When i submit the form the image is stored successfully on my server, but the information relating to the image is not stored.
if(move_uploaded_file($_FILES['photo']['tmp_name'],$target))
{
// Get information from the form
$uploaddesc = $_POST['photo']['description'];
$uploadcat = $_POST['photo']['category']; // error referring to this
$uploadname = $_FILES['photo']['tmp_name']; // error referring to this
// Prepare for user submitted for safe database insert
$uploaddesc = mysqli_real_escape_string($link, $uploaddesc);
$uploadcat = mysqli_real_escape_string($link, $uploadcat);
$uploadname = mysqli_real_escape_string($link, $uploadname);
include $_SERVER['DOCUMENT_ROOT'] . '/includes/shanghai_db.inc.php';
$sql = mysqli_query($link, "INSERT INTO image_detail SET Filename = '$uploadname',
Category = '$uploadcat', Description = '$uploaddesc'");
if(!mysqli_query($link, $sql) )
{
$error = 'Database error storing file information';
include 'error.html.php';
exit();
}
else
{
$output = 'The file and information has been stored successfully';
}
include 'success.html.php';
}
Why does it say that error because in my html form i also added the value=“photo” but no luck
and why on my db column name, Filename says the location to be: C:\wamp\ mp\php5F67.tmp because when i go to C:\wamp\www\Shanghai2010\images\ i can see my image file there, why does it say differente?
Perhaps try this, maybe the file isn’t moving to the right place:
if(move_uploaded_file($_FILES['photo']['tmp_name'],$target))
// All your stuff in here..
}
## Add this line below the IF statement
else {
die("Error moving file");
}
By the way, you always want to do
$_POST[‘name_of_field’],
$_FILE[‘name_of_field’],
The difference is that $_FILE generates a multi-dimensional array containing the data of the upload…
That’s why it’s $_FILE[‘name_of_field’][‘name’]; and so on
You can see all the information by doing print_r($_FILE);
I removed the value attribute on my form and made the adjustment to my php file. After i submitted the form it goes into a blank screen and the details of my image are not saved onto my db, also the image is not saved onto my server. I initially thought by having the following:
would have worked because i’m using the book, Build Your Own Database Driven Website by Kevin Yank 4th Edition, Chapter 12, filestore to help me do this which seemed to work in the book’s tutorial. So i don’t know why i had to change it
Interesting indeed, after adding the else statement, and not really changin the ‘If’ part, i ran my app again, submitted the file and added it’s information accordingly. It was able to save my file onto the server into the correct location, on my db side, the information was saved successfully, however, on the column ‘Filename’ the value for this is:C:\wamp\ mp\php5F67.tmp. Now, the file name is obviously different to that of my image which i’m curious to know, is this got to do with how PHP uses the tmp_name?
Also, on my browser, i get my user generated error: Database error storing file information
which i am guessing has to do with the filename as it does not output my
$output = 'The file and information has been stored successfully';
}
include 'success.html.php';
.....
section. Here is what my IF statement looks liken now with updated changes:
if(move_uploaded_file($_FILES['photo']['tmp_name'],$target))
{
// Get information from the form
$uploaddesc = $_POST['description'];
$uploadcat = $_POST['category'];
$uploadname = $_FILES['photo']['tmp_name'];
//Prepare for user submitted for safe database insert
$uploaddesc = mysqli_real_escape_string($link, $uploaddesc);
$uploadcat = mysqli_real_escape_string($link, $uploadcat);
$uploadname = mysqli_real_escape_string($link, $uploadname);
include $_SERVER['DOCUMENT_ROOT'] . '/includes/shanghai_db.inc.php';
$sql = mysqli_query($link, "INSERT INTO image_detail SET Filename = '$uploadname',
Category = '$uploadcat', Description = '$uploaddesc'");
if(!mysqli_query($link, $sql) )
{
$error = 'Database error storing file information';
include 'error.html.php';
exit();
}
else
{
$output = 'The file and information has been stored successfully';
}
include 'success.html.php';
}
else
{
die("Error moving file");
}