如何使用php,undefined index:images将图像上传到数据库

时间:2017-10-13 06:00:40

标签: php

任何人都可以帮助我在学校的任务,我感到困惑..图像无法存储到数据库。

<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行的图像

3 个答案:

答案 0 :(得分:1)

文件数组中不存在关键图像。 imageUpload是您从html表单传递的name属性。这就是你访问的内容

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

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

另外,请在文件上传模块中添加验证。因为它可以轻松上传特洛伊木马和其他有害文件

答案 1 :(得分:1)

您实际上尝试使用名称&#34; images&#34;来访问表单输入。 - 它不存在。在您的表单中,该字段命名为&#34; imageUpload&#34;

所以改变:

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

要:

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

答案 2 :(得分:0)

确保使用输入字段的名称:

是imageUpload

将此更改为:$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']);