未定义的索引不知道为什么

Undefined index not sure why

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

出于某种原因,我在第 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."; 
 } 

 ?> 

错误信息:

Undefined index: photo in C:\wamp\www\sky_coding\upload.php on line 45, 49 and 55 Undefined index: title in C:\wamp\www\sky_coding\upload.php on line 48

请帮忙 我很困惑,不确定为什么会收到此错误

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

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

至少你需要这样的东西:

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 的攻击,并且还简单地假设数据库查询永远不会失败。

您正在 post 中访问 name,但它应该是 title,如您的表单代码

中所提供的
 $title=$_POST['title'];this should be title

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

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