Hello,
I feel miserable because I spent the last 3 days trying to solve this so I decided to request for help.
I created a form where users can apply for job position through the website. The most challenging part for me is the “upload CV”. the filename IS being displayed on the database…but the file itself is not showing in the destination I requested to code to upload it to (‘/uploads/’).
The upload code was in a different file called “uploadFile.php” and sisnce the form itself is taking action from another file I decided to move the upload code to the action file which is “create-form.php”. and Still no luck…
Well, your code doesn’t actually make sense if you read it out loud. If you successfully inserted your data, you use a die(); and stop anything after that from executing including the uploading a file section. But when you have an error, you allow the file to execute. The logic behind it is very flawed and wrong. This is why I tend to brainstorm before I start a project. I talk to myself out loud what I want the code to do and how I want it to work. If the code I write isn’t what I said out loud, it doesn’t make sense.
Another problem I see is that if you are allowing someone to upload a file, you can’t use $_POST to reference it. You have to use $_FILES. - Never mind, I saw just the first $_POST['uploadCV'] which I assumed you were using to upload the file. A few lines below that, you are using $_FILES['uploadCV'].
Don’t get so hasty. What I would do is save my original work as a backup and reference it to make sure I get most of the stuff I want. Then I would create a whole new file and then rewrite each code by parts starting with the database insert. You’re jumping this too fast and you’re making more mistakes because you’re being too hasty. My boss always says
Haste makes waste
Meaning that you are more likely to make more mistakes if you are in a hurry. Speaking of mistakes, Undefined index errors typically mean that you are referencing a variable that was never created. This also happens when you are trying to reference a $_POST, $_GET, or $_FILES index which was never created.
Take your time.
I’ll help you start your code.
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Start your validation.
} else {
// Change this line to whatever your form file was called.
header('Location: back_to_the_form_file.php');
die;
}
“uploadCV” is a file input in your form, so it will appear in the $_FILES array, not in the $_POST array. So when you try to use it from $_POST, you get “undefined index”. You could have a separate form field of that same name but of a different type, perhaps a text box or a selection, which would appear in $_POST, but you haven’t. ETA : As @spaceshiptrooper said earlier, in fact.
Agreed. What I would do in normal circumstances is make sure the file is uploaded first before I insert the name and data. If it isn’t uploaded, I wouldn’t allow the data to be inserted. However, if the CV is optional, then you would have to figure out how you want it to proceed.
The html is correct if you want to upload a file, but you should not reference it as $_POST['uploadCV'], it is $_FILES['uploadCV'], as you use after the query in the first code you posted.
That’s not weird, it’s that setting that tells your browser to support multiple different types of input, specifically to add support for the input type="file" tag. It might have got rid of your error message, but it won’t allow your file to upload properly AFAIK.
Try it - var_dump($_POST) and see if it gives you enough information to upload the file.
Great, I got things to work out. but now something else, the files are being uploaded which is very very great!
but the file name is not being displayed on the DB. any ideas?
It looks as though a text editor is being used to try and get the script to perform correctly. If so then try stepping through the script one line at a time and verifying the script is indeed showing the expected values at a particular point and only moving to the next break point once the values are correct.
<?php
// break point
var_dump( $variableToInspect ); die;
This debug method is tedious but very thorough and far better than randomly changing a single line and hoping the desired result will be resolved.