Tim Tim - 5 months ago 7
HTML Question

php undefined index with multiple file upload

I have this code but I can't get what's wrong with it. Any help will be appreciated. Please don't be too quick to mark it as a duplicate without giving me a solution as I have been through all question that are similar and they do not answer my problem as I have already checked if file is uploaded be for processing it and still get this error UNDEFINED INDEX code:

<!DOCTYPE html>
<html>
<body>

<form role="form" method="post" action="test2.php">
<div class="form-group">
<div class="row">
<div class="col-xs-8">
<label class="control-label">Call Sign</label>
<input type="text" class="form-control" name="call_sign" />
</div>
<div class="col-md-4">
<label class="control-label">Picture</label>
<img src="Koala.jpg" alt="with responsive image feature" class="img-responsive img-circle">


</div>
</div>
<div class="row">
<div class="col-xs-8">
<label class="control-label">Driver First Name</label>
<input type="text" class="form-control" name="first_Name" />
</div>
</div>
<div class="row">
<div class="col-xs-8">
<label class="control-label">Driver Last Name</label>
<input type="text" class="form-control" name="last_Name" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-8">
<label for = "fileToUpload">File input</label>

<input type="file" name="fileToUpload" id="fileToUpload" >
</div>
</div>
<div class="row">
<div class="col-xs-8">
<label class="control-label">Location</label>
<input type="text" class="form-control" name="Location" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-4 selectContainer">
<label class="control-label">Company Shirt</label>
<select class="form-control" name="CShirt">
<option value="">Choose </option>
<option value="Good">Good</option>
<option value="Poor">Poor</option>
<option value="UniformNotWorn">Uniform Not Worn</option>
<option value="Untidy">Untidy</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-xs-4 selectContainer">
<label class="control-label">Trousers</label>
<select class="form-control" name="Trousers">
<option value="">Choose </option>
<option value="Clean/Smart">Clean/Smart</option>
<option value="Acceptable">Acceptable</option>
<option value="Jeans/Casual">Jeans/ Casual</option>
<option value="Untidy">Untidy</option>
</select>
</div>
</div>

<div class="form-group">
<div class="col-xs-4 selectContainer">
<label class="control-label">General Appearance</label>
<select class="form-control" name="Appearance">
<option value="">Choose </option>
<option value="Good">Good</option>
<option value="Fair">Fair</option>
<option value="Poor">Poor</option>
</select>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="control-label">In the Vehicle</label>
<div class = "checkbox">
<label><input type = "checkbox" value="Tea/Coffe" name="In_the_Vehicle"> Tea/Coffe</label>
</div>
<div class = "checkbox">
<label><input type = "checkbox" value="Eating" name="In_the_Vehicle"> Eating</label>
</div>
<div class = "checkbox">
<label><input type = "checkbox" value="Smoking" name="In_the_Vehicle"> Smoking</label>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label">Assessor Comments</label>
<div>
<textarea class="controlline" name="Assessor_Comments" rows="8"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label">Driver Comments</label>
<div>
<textarea class="controlline" name="Driver_Comments" rows="8"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label">Kit Required</label>
<div>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="DMN Shirts" /> DMN Shirts
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="DMN Jacket" /> DMN Jacket
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="BMW Jacket" /> BMW Jacket
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="Hi-Viz-Jacket" />Hi-Viz-Jacket
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="ID Badge" /> ID Badge
</label>
</div>
</div>
<div class="form-group">
<div>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="Tyre_Guage" /> Tyre Guage
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="Trade_Plate_Bands" /> Trade Plate Bands
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="MiVIS_Ruler/Kit" /> MiVIS Ruler/Kit
</label>
</div>
</div>
<div class="form-group">
<label class="control-label">Training Wanted</label>
<div>
<label class="radio-inline">
<input type="radio" name="Training_Wanted" value="Yes" /> Yes
</label>
<label class="radio-inline">
<input type="radio" name="Training_Wanted" value="No" /> No
</label>
</div>
</div>


<button type="submit" class="button" name="submit">Submit</button>
</form>

</body>
</html>
<?php


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

$query ="INSERT INTO spot_checkdetails(location,CShirt,trouser,appearance,kitReq,Training,fk1_callSIgn)
VALUES('$_POST[Location]','$_POST[CShirt]' ,'$_POST[Trousers]' ,'$_POST[Appearance]','$_POST[Kit_Required]' ,'$_POST[Training_Wanted]','$_POST[call_sign]')";
$result = mysqli_query($connection, $query);
if (!$result)die ("database acess failed".$connection->error);
$foreignk="select id from spot_checkdetails order by id desc limit 1";
$result = mysqli_query($connection, $foreignk);

while($row = $result->fetch_assoc())
{

$reqry=$row["id"];

}

$query ="INSERT INTO spotcheckcomment(assCom,driCom,spot_checkdetails_id)VALUES('$_POST[Assessor_Comments]','$_POST[Driver_Comments]','".$reqry."')";
$result = mysqli_query($connection, $query);

$foreign2="select id from spotcheckcomment order by id desc limit 1";

$result = mysqli_query($connection, $foreign2);

while($row = $result->fetch_assoc())
{

$reqry2=$row["id"];

}
$foreign3="select fk1_callSIgn from spot_checkdetails order by id desc limit 1";

$result = mysqli_query($connection, $foreign3);

while($row = $result->fetch_assoc())
{

$reqry3=$row["fk1_callSIgn"];

}
if(!empty($_POST)){ // **check weather the form is submitted or not**
// Where the file is going to be placed
$target_path = "uploads/";
$random_digit=rand(0000,9999)."_";
$jobnumber= $reqry3."_".$random_digit;


//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables

$new_file_name =$jobnumber. basename($_FILES["fileToUpload"]["name"]);
//$target_file = $target_dir .$new_file_name;

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path .$new_file_name; // basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['fileToUpload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}

$query ="INSERT INTO spotcheckimagepath(imgname,imagepath,spot_check_comment_id)VALUES('".$new_file_name."','".$target_path."','".$reqry2."')";
$result = mysqli_query($connection, $query);
}
?>


the ERROR:) Notice: Undefined index: fileToUpload in

Answer

As w3schools.com says you need to add enctype="multipart/form-data" to you form.

  • multipart/form-data

    No characters are encoded. This value is required when you are using forms that have a file upload control

So change this line

<form role="form" method="post" action="test2.php">

to this one

<form role="form" method="post" action="test2.php" enctype="multipart/form-data">

Also you can see here how check if image was uploaded