-2

For some reason my files are not uploading to my database. I have my file upload turned on in my php.ini file and have tried uploading files less than 2 mb but no luck so far. I am pretty new to programming so please bear with me.

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Upload PDF & Word files to Database</title>
</head>
<body>
<?php
$dbh = new PDO("mysql:host=localhost;dbname=database", "user", "root");
if (isset($_FILES['myfile'])) {
    $name = $_FILES['myfile']['name'];
    $mime = $_FILES['myfile']['type'];
    $data = file_get_contents($_FILES['myfile']['tmp_name']);
    $stmt = $dbh->prepare("INSERT INTO myblob VALUES('',?,?,?)");
    $stmt->bindParam(1, $name);
    $stmt->bindParam(2, $mime);
    $stmt->bindParam(3, $data, PDO::PARAM_LOB);
    $stmt->execute();

}
?>
<form method="post" enctype="multipart/form-data">
   <input type="file" name="myfile"/>
   <input name="btn" type="hidden" value="Value">
   <input type="submit" value="Upload">
</form>

</body>
</html>   

table structure

  • Why are you trying to insert `''` to an Int type column with auto_increment? – Alon Eitan Dec 02 '18 at 20:36
  • 1
    `
    ` <<< see that? You closed the form too early.
    – Funk Forty Niner Dec 02 '18 at 20:43
  • @DarkMukke Seeing the deleted answer, that was not my downvote you got. Thought you might like to know. I was going to say that under it, but it was too late. – Funk Forty Niner Dec 02 '18 at 20:44
  • @FunkFortyNiner no but if my answers are wrong or off topic because i missed the actual answer i prefer to keep it clean and remove it – DarkMukke Dec 02 '18 at 20:47
  • @DarkMukke [This is what I meant earlier...](https://stackoverflow.com/questions/53584270/files-are-not-uploading-to-a-database#comment94032353_53584270) in my comment above about missing something (under your deleted answer), being a comment I left to the OP. – Funk Forty Niner Dec 02 '18 at 20:47
  • If all this was being closing off the form too early, then this should be closed as a typographical error, IMHO. It has no added worth to the Stack Q&A, – Funk Forty Niner Dec 02 '18 at 20:49
  • @AlonEitan I can't see that `('',?,?,?)` failing. This is a typo question; see my comment under yours. – Funk Forty Niner Dec 02 '18 at 20:55
  • 2
    One has to wonder where the OP is in all this. – Funk Forty Niner Dec 02 '18 at 20:56
  • @FunkFortyNiner I flagged it as a type because of the `
    ` part, but I personally prefer to specify the columns in the query itself because if the op changes the structure of the table then it might not work
    – Alon Eitan Dec 02 '18 at 20:59
  • 1
    Also I wonder how it will be displayed with a space in the id column `$row[' id']` – DarkMukke Dec 02 '18 at 21:00
  • 2
    @AlonEitan Right you are, it is good practice to do so :) However, I only have one vote to close and I'm not going to blow it on a silly little (potential) typo *lol!!* – Funk Forty Niner Dec 02 '18 at 21:00
  • @DarkMukke Oh, nice catch on the `$row[' id']`, I hadn't noticed that. That in its own right with error reporting/error handling would have thrown an undefined index notice. – Funk Forty Niner Dec 02 '18 at 21:02
  • 1
    At this point in time, I feel that given the inputs outside the form and the space in the `$row[' id']` both qualify as undefined indexes and is worthy of a vote to close as such. Edit: and another added. Consult those. – Funk Forty Niner Dec 02 '18 at 21:04
  • @FunkFortyNiner now I am confused as to why this was closed as a duplicate but i understand why you would close this – DarkMukke Dec 02 '18 at 21:07
  • @DarkMukke Because, both are worthy of undefined indexes along with not checking for errors on the PDO side of things, which I am pretty sure they are not checking for them. The close stands, far as I'm concerned. – Funk Forty Niner Dec 02 '18 at 21:08
  • @AlonEitan,@FunkFortyNiner,@DarkMukke. Hey guys I feel like I am pretty close and have entered those lines of code into the page that you suggested. Could you lend anymore expertise? – Nick Wagner Dec 05 '18 at 00:08

1 Answers1

-1

This is because

if (isset($_POST['btn'])) {

will never happen. A button does not post a value

replace it with

if (isset($_FILES['myfile'])) {

or replace the submit with

<input name="btn" type="hidden" value="somevalue">
<input type="submit" value="Upload">

The later will keep your php as is but post a value with the name btn

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
DarkMukke
  • 2,469
  • 1
  • 23
  • 31
  • You missed something. – Funk Forty Niner Dec 02 '18 at 20:43
  • Seeing you undeleted the answer, I think the person who gave you that downvote disagreed with you in regards to saying that the `if (isset($_POST['btn'])) {...}` will never happen. They do have an submit input of that name. What that does is to make sure that it was clicked without executing what's inside that conditional statement. But again, this is a typo question. – Funk Forty Niner Dec 02 '18 at 20:54
  • _"will never happen. **A button does not post a value**"_ - This is wrong in the current context, it could be [correct](https://www.w3.org/TR/html401/interact/forms.html#h-17.13.2) if you submit the form using js, i,e - not by clicking on the submit button – Alon Eitan Dec 02 '18 at 20:55
  • I had to un-delete it because half the comments on the post are about it, but yes, it is incorrect and should be down voted or deleted. – DarkMukke Dec 02 '18 at 20:57
  • `if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_FILES['myfile']['size'] > 0)` – levye Dec 02 '18 at 21:58
  • when you use firebug or chrome debuger, you can see the post variables, google it if you want to know more about it. check the post variables to see if the form is submitting correctly – DarkMukke Dec 03 '18 at 01:07
  • If I use if `(isset($_FILES['myfile'])) {` what else in my php code would have to change? – Nick Wagner Dec 03 '18 at 18:08
  • @NickWagner can you update your post with how the code is currently ? Also make sure you have no other errors in it (like `$row[' id']`). I will update my answer accordingly – DarkMukke Dec 03 '18 at 18:50
  • @DarkMukke. I have updated what I currently have. I got rid of the error of `$row[' id']` . – Nick Wagner Dec 03 '18 at 20:02