2017-10-13 78 views
0

任何人都可以幫助我在學校的任務,我困惑的是,圖像不能存儲到數據庫。如何上傳圖像到數據庫使用php,undefined索引:圖像

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

Category : T-SHIRT 

Choose Size 
<label for="sizes"><span> 
<input type="radio" name="sizes" value="S" checked="checked"/>S 
<input type="radio" name="sizes" value="M" />M 
<input type="radio" name="sizes" value="L" />L 
<input type="radio" name="sizes" value="XL" />XL 
</span> 
</label> 

Fabric Type 
<select name="fabric"> 
    <option value="Cotton Combed">Cotton Combed</option> 
    <option value="Cotton Carded">Cotton Carded</option> 
    <option value="Polyester/PE">Polyester/PE</option> 
    </select> 

Total 
<input type="number" name="total" min="1" max="100"> 
<input type="file" name="imageUpload" id="imageUpload"> 
<input value="Submit" type="submit" name="submit"> 
</form> 

和這個doOrder.php

<?php 
    require_once("connect.php"); 

    $nama_file = $_FILES['images']['name']; 
    $sizes = $_POST['sizes']; 
    $fabric = $_POST['fabric']; 
    $total = $_POST['total']; 

    move_uploaded_file($_FILES['images']['tmp_name'], "images/".$_FILES['images']['name']); 


    $simpan = mysql_query("INSERT INTO pesanan(category, sizes, fabric, total,images) VALUES('T-SHIRT','$sizes','$fabric','$total','$nama_file')"); 

    echo "Your add has been submited...."; 

?> 

誤差是

說明:未定義指數:在第4行的通知圖像:未定義指數: 圖像中的線9注意:未定義索引:在線9圖像

+0

你''具有'名稱= 「imageUpload」',爲什麼你在PHP中將其稱爲'$ _FILES ['images']'? – Styx

回答

1

關鍵圖像不存在於文件數組中。 imageUpload是你作爲html表單的名稱屬性傳遞的內容。所以這就是你要訪問的內容

$nama_file = $_FILES['imageUpload']['name'];//imageUpload 

move_uploaded_file($_FILES['imageUpload']['tmp_name'], "images/".$_FILES['imageUpload']['name']); 

此外請將驗證添加到您的文件上傳模塊。因爲它可以很容易地上傳木馬和其他有害文件

+0

ooh my god> _ <非常感謝你...... ~~~~~ –

+0

很酷。請將我的答案標記爲已接受 – Akintunde007

+1

好吧,等10分鐘後:) –

1

你實際上試圖訪問名稱爲「圖像」的表單輸入 - 它不存在。在你的表單,字段被命名爲 「imageUpload」

因此改變:

$nama_file = $_FILES['images']['name']; 

要:

$nama_file = $_FILES['imageUpload']['name']; 
0

確保您在使用輸入字段的名稱:

這是圖片上傳

更改爲:$nama_file = $_FILES['images']['name'];

$nama_file = $_FILES['imageUpload']['name']; 

也:

move_uploaded_file($_FILES['image']['tmp_name'], "images/".$_FILES['image']['name']); 

到:

move_uploaded_file($_FILES['imageUpload']['tmp_name'], "images/".$_FILES['imageUpload']['name']);