【问题标题】:Undefined index not sure why未定义的索引不知道为什么
【发布时间】:2016-01-28 14:54:06
【问题描述】:

创建一个应该展示几个不同电视节目的小型网站。我有一个页面应该允许用户输入新的电视节目名称和电视节目的图片。

由于某种原因,我在第 41、44、45 和 52 行遇到未定义索引错误。代码如下:

<form action="upload.php" method="POST" enctype="multipart/form-data">
    Programme Title: <input type="text" name="title">   </br> 
    Select photo to upload:<input type="file" name="photo" id="photo">  <br/>
    <input type="submit" value="Upload" name="submit">
</form>

<?php

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']);  --- ERROR line 41 

 //This gets all the other information from the form 
 $title=$_POST['name']; --- ERROR line 44
 $pic=($_FILES['    ']['name']);  --- ERROR line 45


 //Writes the information to the database 
 mysqli_query($mysqli, "INSERT INTO programmes VALUES ('', '$title', '', '$pic')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  --- ERROR line 52
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 

 ?> 

错误信息:

未定义索引:照片在 C:\wamp\www\sky_coding\upload.php 上线 45、49 和 55 未定义索引:标题在 C:\wamp\www\sky_coding\upload.php 在第 48 行

请帮忙 我很困惑,不知道为什么会出现这个错误

任何信息都会有所帮助 谢谢!

【问题讨论】:

  • 请标出线条。
  • @NiranjanNRaju 如何标记线条?
  • 只需在 cmets 中写一行,例如 error line
  • 我已经更新了帖子
  • 当您编写有关错误的问题时,总是包含错误的详细信息。将错误报告添加到您打开 PHP 标记之后的文件顶部,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1); 然后是您的其余代码,以查看它是否产生任何结果。您想通过这一行实现什么 $pic=($_FILES[' ']['name']);

标签: php mysql image uploading


【解决方案1】:

您从不费心检查表单是否已提交,甚至文件是否已上传。你只是无条件地执行表单处理代码。

至少你需要这样的东西:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   // form was submitted
   if (isset($_FILES['photo'])) {
         if ($_FILES['photo']['error'] === UPLOAD_ERR_OK) {
              ... file was successfully uploaded, process it
         } else {
              die("File upload failed with error " . $_FILES['photo']['error']);
         }
   }
   etc... etc... etc...
}

您也容易受到sql injection attacks 的攻击,并且还只是假设数据库查询永远不会失败。

【讨论】:

  • 是的,我犯了一个愚蠢的错误。这设法让一切正常
【解决方案2】:

您在帖子中访问name,但它应该是您的表单代码中提供的title

 $title=$_POST['title'];this should be title

您将留下空索引,因此将索引添加为photo

 $pic=($_FILES['photo']['name']); you have not added any index here.

【讨论】:

  • 剧照给出了“照片”和“标题”的未定义索引错误
  • 您能总结一下您引用的内容吗?这违反了 stackoverflow 发布指南。
  • 对不起,你是什么意思?
  • 我为提交按钮添加了 Isset 功能,错误消失了。图像正在上传,但是一旦我点击上传按钮,它就会显示 Undefined index: Uploadedfile in C:\wamp\www\sky_coding\upload.php 在第 59 行
  • photo替换uploadedfile
猜你喜欢
  • 2014-07-27
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
相关资源
最近更新 更多