0

This was working but stopped afterwards.
Other fields have no issue submitting.
When I press submit the other fields are added to the database but this one is not.
To prevent confusion the value that matters is photo_url not photo_dir.
Further clarification: photo_dir is the value of a drop down menu with a compass direction. All I want is to store the file name in the DB under column photo_url.

This is the form field

<label for="photo_url">Upload:</label>
<input type="file" name="photo_url">

This is the SQL

$sql="INSERT INTO photo(photo_project_id,photo_section,photo_subsection,photo_date,photo_post,photo_desc,photo_url,photo_dir)VALUES('$_POST[photo_project_id]','$_POST[photo_section]','$_POST[photo_subsection]','$_POST[photo_date]',now(),'$_POST[photo_desc]','$_POST[photo_url]','$_POST[photo_dir]')";

The file is also being uploaded to the server which is working without issue. Though I would like to be able to rename them to datetime() but that is a topic for a different day.

Karim
  • 15
  • 7
  • As an aside, you should never pass POST variables straight into an SQL query. You code as it is currently is wide open to SQL injection attacks. – solarissmoke May 18 '16 at 04:15
  • I will be preparing the statements in the final version. However if I did not all that would happen is that you would get your own data because of the application. If you can link me to a step by step guide however that would be great. – Karim May 18 '16 at 04:26
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – miken32 May 18 '16 at 05:05
  • That is asking what it means. This is asking g what is causing it. – Karim May 18 '16 at 10:22

2 Answers2

0

You can access to file element with $_FILE like this

$_FILES['photo_dir']

in the first step print_r this because it has another child

Majid Abbasi
  • 1,531
  • 3
  • 12
  • 22
  • Same error. Doubt that was it since it was working before with $_post. – Karim May 18 '16 at 04:02
  • Do you have enctype="multipart/form-data" in your form? print_r($_FILES["photo_dir"]) it should give you data of uploaded file – Majid Abbasi May 18 '16 at 04:05
  • Yes I do. The weird thing is the file upload script uses the same $_FILES["photo_dir"] and it has no issues working. I am using the code in the w3schools link to handle the upload (with some modifications). – Karim May 18 '16 at 04:07
  • you can not save $_FILES["photo_dir"] to db because it is an array, you can save one index of that for example $_FILES["photo_dir"]["tmp_name"] – Majid Abbasi May 18 '16 at 04:08
  • @MajidAbbasi, he put his input file name as `photo_url` so definitely he wouldn't get anything inside `$_FILES['photo_dir']` – Maha Dev May 18 '16 at 04:12
  • Sorry for the confusion photo_url is the value we are looking at not photo_dir – Karim May 18 '16 at 04:12
0

First of all your should have this attribute enctype="multipart/form-data" for using input type file, Should look like this :

<form action="" enctype="multipart/form-data">

After form submit, check :

if(isset($_FILES['photo_url'])){
 $file_name = $_FILES['photo_url']['name'];
}

Rewrite query then :

$sql="INSERT INTO photo(photo_project_id,photo_section,photo_subsection,photo_date,photo_post,photo_desc,photo_url,photo_dir)VALUES('$_POST[photo_project_id]','$_POST[photo_section]','$_POST[photo_subsection]','$_POST[photo_date]',now(),'$_POST[photo_desc]','$_POST[photo_url]','$file_name')";
Maha Dev
  • 3,915
  • 2
  • 31
  • 50
  • photo_dir is completely unrelated and has to do with in which direction the photo was taken! I should clarify that photo_url is the value I am looking at. – Karim May 18 '16 at 04:10
  • For the rewrite should it be: $sql="INSERT INTO photo(photo_project_id,photo_section,photo_subsection,photo_date,photo_post,photo_desc,photo_url,photo_dir)VALUES('$_POST[photo_project_id]','$_POST[photo_section]','$_POST[photo_subsection]','$_POST[photo_date]',now(),'$_POST[photo_desc]','$file_name','$_POST[photo_dir]')"; – Karim May 18 '16 at 04:13
  • I don't know what you are getting inside `$_POST[photo_dir]`. What i have created a variable that will return you the file name. You can append your image directory with file name to save full path inside database. – Maha Dev May 18 '16 at 04:15
  • photo_dir is the value of a drop down menu with a compass direction. All I want is to store the file name in the DB under column photo_url – Karim May 18 '16 at 04:17