2015-06-30 33 views
1

我在代碼中提到「錯誤行」表示行顯示此錯誤「注意:未定義的索引:C:\ xampp \ htdocs \ Lab \ register2.php中的文件在行33」 。但在我的數據庫中增加的數據意味着主鍵(id)。我需要在PHP中解決它請幫助我。文件上傳未定義索引錯誤php

<?php 

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

$fullname=$_POST["fullname"]; 

$file = rand(1000,100000)."-".$_FILES['file']['name']; //Error line 
$file_loc = $_FILES['file']['tmp_name']; //Error line 


$info = pathinfo($_FILES['file']['name']); //Error line 
$ext = $info['extension']; //Error line 
$newname = $userid.".".$ext; //Error line 

$target = 'uploads/'.$newname; 
move_uploaded_file($_FILES['file']['tmp_name'], $target); //Error line 






require_once "config.php"; 
$db=get_connection(); 
$sql="INSERT INTO user(Path) VALUES('$newname')"; 
mysql_query($sql); 
} 

?> 




<!DOCTYPE html> 
<html> 
    <title></title> 

    <body> 
     <center> 
     <form action="#" method ="POST"> 

     Full Name : <input type="text" name="fullname"/><br/> 
     Phone : <input type="number" name="phone"/><br/> 
     Address :<textarea rows="4" cols="30" name="textin"></textarea><br> 
     Profile Picture : <input type="file" name="file" /> <button type="submit" name="btn_upload">Upload Image</button><br/> 


     <input type= "submit" name ="back" value="Back"/><br/> 
     <input type= "submit" name ="next1" value="Next"/> 


      </form> 

     </center> 
    </body> 

    </html> 
+0

嘗試在您的表單元素中添加'enctype =「multipart/form-data」'。 http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean – chris85

+0

你剛剛更新了你的問題,而不是在這裏問以前的評論?如果表單沒有提交文件,索引不會存在。或者我想你可以做一個空/ isset檢查似乎毫無意義,雖然當你可以解決問題時......你也可以關閉錯誤報告或抑制錯誤(我不會採取的路線)。 – chris85

回答

1

您的表單應該有enctype="multipart/form-data"作爲表單元素。

所以,你應該有

<form action="#" method ="POST" enctype="multipart/form-data"> 

如果一個文件的內容與形式,文件輸入應該通過適當的內容類型來確定提交(例如,「應用程序/八位字節流」)

來源和學習更多關於Form Elements here

注:

從表單元素中刪除#符號,因爲它不是必需的。

建議:

不要使用w3schools您的所有基礎學習,嘗試使用 PHP The Right Way

0

的enctype屬性指定如何形成的數據應該將它提交給server.The時編碼僅當method =「post」時纔可以使用enctype屬性。

<body> 
    <center> 
    <form action="#" method ="POST" enctype="multipart/form-data"> 
    // Your Code 
     </form> 

    </center> 
</body> 
0
<form action="#" method ="POST" enctype="multipart/form-data"> 
    //Your form Code 
</form> 

enctype="multipart/form-data"這將使你的表單提交的文件(圖片,PDF,Office文件,等...)

額外注意

php.ini(以線912 )

file_uploads=On

Read This Tutorial

+0

那裏有很多其他的教程。此外,enctype已在其他2個答案和評論中提及。 – chris85