Search code examples
phpfileundefined-index

Undefined Index when using $_FILES to upload a picture


I'm in the process of creating an application using PHP, I'm no expert at it at all but I am slowly getting it done.

I have come across a problem when allowing a user to upload information along side an image.

Below is my form I set up to allow the user to input all the information and to choose an image they wish to upload:

<form action="NewOwnedItem.php" method="POST">
      <div class="input-row">
        <label>Item's Name</label>
        <input type="text" name="Name" placeholder="Enter your item's name">
      </div>
      <div class="input-row">
        <label>Image</label>
        <input type="file" name="file_img">
      </div>
      <div class="input-row">
        <label>Description</label>
        <input type="text" name="Description" placeholder="Enter your item's description">
      </div>
      <div class="input-row">
        <label>Quantity</label>
        <input type="number" name="Quantity" min="1" max="99"  placeholder="Enter the quantity you own of this item">
      </div>
      <div class="input-row">
        <label>Price</label>
        <input type="text" name="Price" placeholder="Enter the price you paid for this item">
      </div>
      <div class="input-row">
        <label>Condition</label>
        <select name="Condition">
          <option value="Very Poor">Very Poor</option>
          <option value="Poor">Poor</option>
          <option value="Fair">Fair</option>
          <option value="Good">Good</option>
          <option value="Very Good">Very Good</option>
          <option value="Excellent">Excellent</option>
          <option value="Near Mint">Near Mint</option>
          <option value="Mint">Mint</option>
        </select>
      </div>
      <div class="input-row">
        <label>Date Purchased</label>
        <input type="date" name="DatePurchased">
      </div>

      <input type="submit" value="Add Item" name="submit">

    </form>

Below is the PHP code to take this information and store it within a database (connection to database is earlier in the file so not shown here). I'm wanting it so that the image URL is stored in the database whilst the actual image is stored in an "upload" file on the server, I followed a tutorial for that part of the code however the errors still appear.

Notice: Undefined index: file_img in /web/users/n3071633/FYP/NewOwnedItem.php on line 50 Notice: Undefined index: file_img in /web/users/n3071633/FYP/NewOwnedItem.php on line 51

Those are the errors that appear, below is the PHP code.

<?php

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

      $Name = mysql_escape_string($_POST['Name']);
      $Description = mysql_escape_string($_POST['Description']);
      $Quantity = mysql_escape_string($_POST['Quantity']);
      $Price = mysql_escape_string($_POST['Price']);
      $Condition = mysql_escape_string($_POST['Condition']);
      $DatePurchased = mysql_escape_string($_POST['DatePurchased']);

      $filetmp = $_FILES["file_img"]["tmp_name"];
      $filename = $_FILES["file_img"]["name"];
      $filepath = "uploads/".$filename;

      move_uploaded_file($filetmp,$filepath);

      mysql_query("INSERT INTO `CS_Items` (`Item_ID`, `Name`, `Image`, `Description`, `Quantity`, `Price`, `ItemCondition`, `DatePurchased`, `User_ID`, `ItemStatus`) VALUES (Null, '$Name', '$filepath', '$Description', '$Quantity', '$Price', '$Condition', '$DatePurchased', '$login_session', 'Owned') ");
    }
  ?>

What confuses me is that all the other parts of the form work however the ones that include $_FILES appear to the be the ones that are apparently not defined?

I have tried looking at other questions and answers however the answers seem to be things that are already within my code? Any help will be appreciated


Solution

  • You forgot to add enctype="multipart/form-data" in your <form action="NewOwnedItem.php" method="POST"> so without that you never get $_FILES to be worked.

    so make it like:-

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