2016-07-06 34 views
0

我有這樣的代碼,但我不能得到什麼地方錯了。任何幫助將不勝感激。請不要太快將它標記爲重複而不給我解決方案,因爲我已經通過了所有類似的問題,並且他們沒有回答我的問題,因爲我已經檢查過上傳文件是否用於處理它並仍然得到這個錯誤未定義指數:PHP未定義指數上傳

HTML代碼:

<!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); 
    } 
?> 

的錯誤:)注意:未定義的我ndex:fileToUpload在

+2

添加'ENCTYPE =「多部分/格式數據」'以形成標籤 – Saty

+0

正如前面提到的,使用'多/表單data',也寫在文件開頭的PHP代碼 - 這是不是你的情況危急,但是這大概是「良好做法」 –

回答

1

由於w3schools.com說,你需要添加enctype="multipart/form-data"你形成。

  • 的multipart/form-data的

    沒有字符被編碼。當您使用的是有一個文件上傳控件形式

所以這條線

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

改變這一

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

,則需要此值還可以看到here如何檢查,如果圖像被上傳

1

更改如下:

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

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

還要檢查是否變種試圖訪問它之前就存在,可以使用

if(isset($_FILES['fileToUpload']){do something}else{error!}

此外,建議您在t之前移動您的PHP代碼他的HTML。