2013-01-31 47 views
0

我一直试图解决这个错误数小时。任何人都可以帮助我,错误在哪里?谢谢。我试图上传一个图像到MySQL数据库。 PHP错误日志直接给我的错误:未定义索引上传文件。谢谢!将图像上传到mysql数据库时出错

test.php的

Flavor: <input id="flavor" type="text" name="flavor"> 
    Upload image: <input type="file" name="uploadedfile" id="uploadedfile" /> 

这是我的代码:

<?php 
include_once "mysqli.connect.php"; 

if(isset($_POST['Add'])) 
{  
     $flavor = $_POST['flavor']; 
     $target_path = "images/"; 
     $image = basename($_FILES['uploadedfile']['name']); 
     $target_path = $target_path . $image; 


     if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
     { 

      $sql = "INSERT INTO flavor (flavorname, image) VALUES('$flavor','$target_path')"; 
      $result = $mysqli->query($sql);  
      echo"<script type='text/javascript'>alert('Flavor added!')</script>";   
     } 
     else 
     { 
      echo"<script type='text/javascript'>alert('Error! Can't proceed.')</script>"; 
     } 

     }  
?> 
+1

在上传之前检查'isset($ _ FILES)' – DON

+0

您很容易受到SQL注入攻击!了解准备好的陈述以保护自己! – Quentin

+0

form enctype =“multipart/form-data”action =“__ URL__”method =“POST”> .... – ZigZag

回答

0

你实际上并没有上传文件到数据库中。
整个问题与数据库无关。

因此,要使上传成功,请确保您的表单中有enctype="multipart/form-data"属性。
请参阅manual page了解相关信息。