0

So i've been trying to make a form with an IMG uploader. The db is working and i can send the information to the database. When I tried to implement the image uploader it gives me this print: So in the form I only added the buttom to upload image, added name and on the post method i tried to upload the image before adding the data.

I did a print, and this is what it came out: Field 'imgdire' doesn't have a default value

Can someone tell me what's wrong?

PHP

 **if($_SERVER["REQUEST_METHOD"] == "POST"){
  // Validate album

   $UploadedFileName=$_FILES['uploadimage']['name'];
   if($UploadedFileName!='')
   {
   $upload_directory = "uploads/";
   $TargetPath=time().$UploadedFileName;
    if(move_uploaded_file($_FILES['files']['tmp_name'], 
  $upload_directory.$TargetPath)){    
     $QueryInsertFile="INSERT INTO album SET imgdire='$TargetPath'"; 

   }
}

Send Information to database

// Check input errors before inserting in database
if(empty($artist_err) && empty($album_err) && empty($stock_err) && 
 empty($price_err) && empty($genre_err)){

    // Prepare an insert statement
    $sql = "INSERT INTO album (nomealbum, stock, price, genero, nomeinter) VALUES (?, ?, ?, ?, ?)";

    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "sssss", $param_album, $param_stock, $param_price, $param_genre, $param_artist);

        // Set parameters
        $param_album = $album;
        $param_stock = $stock;
        $param_price = $price;
        $param_genre = $genre;
        $param_artist = $artist;


        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){

            // Redirect to admin page
            header("location: admin.php");
        } else{
           print mysqli_error($link);
            echo "Algo não correu bem, por favor tente mais uma vez";
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}

// Close connection
mysqli_close($link);
}

FORM

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

  <div class="form-group <?php echo (!empty($artist_err)) ? 'has-error' : 
  ''; ?>">
  <label>Intérprete</label>
  <input  type="text" name="inter" class="form-control" placeholder="Nome do 
  Artista" value="<?php echo $artist; ?>">
  <span class="help-block"><?php echo $artist_err; ?></span>
</div>


 <div class="form-group <?php echo (!empty($album_err)) ? 'has-error' : ''; 
  ?
 >">
  <label>Nome do Albúm:</label>
  <input  type="text" name="aname" class="form-control" placeholder="Album" 
  value="<?php echo $album; ?>">
  <span class="help-block"><?php echo $album_err; ?></span>
  </div>


    <div class="form-group <?php echo (!empty($stock_err)) ? 'has-error' : 
    ''; ?>">
  <label>Stock</label>
  <input  type="text" name="stocks" class="form-control"  placeholder="" 
  value="<?php echo $stock; ?>">
  <span class="help-block"><?php echo $stock_err; ?></span>
</div>

<div class="form-group <?php echo (!empty($price_err)) ? 'has-error' : ''; ?
>">
  <label>Preço</label>
  <input  type="text"  name="prices" class="form-control"  placeholder="" 
value="<?php echo $price; ?>">
  <span class="help-block"><?php echo $price_err; ?></span>
</div>

<div class="form-group <?php echo (!empty($genre_err)) ? 'has-error' : ''; ?
>">
  <label>Género</label>
  <input type="text" name="genres"  class="form-control" placeholder="" 
 value="<?php echo $genre; ?>">
  <span class="help-block"><?php echo $genre_err; ?></span>
</div>
<div class="form-group">
<input type="file" name="uploadimage">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">

 </div>
 </form>


</div>
Andre FIlipe
  • 63
  • 1
  • 8
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/q/4261133/6521116) – LF00 Dec 08 '17 at 01:54

2 Answers2

1

You are accessing the wrong keys in move uploaded file line Should be

if(move_uploaded_file($_FILES['uploadimage']['tmp_name'])){//uploadimage is the key 

}
Rotimi
  • 4,783
  • 4
  • 18
  • 27
-1
move_uploaded_file($_FILES["image"]["tmp_name"],
    "uploads/" . $_FILES["image"]["name"]);
$location1 = $_FILES["image"]["name"];
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
jdk
  • 1