创建一个应该展示几个不同电视节目的小网站。我有一个页面,应该允许用户输入新的电视节目名称和电视节目的图片。
由于某种原因,我在行上获得了未定义的索引错误: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未定义的索引:标题 第48行的C:\ wamp \ www \ sky_coding \ upload.php
请帮忙 我很困惑,不知道为什么我得到这个错误
任何信息都会有所帮助 谢谢!
答案 0 :(得分:2)
您从未打扰过检查表单是否已提交,或文件是否已上传。您只是无条件地执行表单处理代码。
至少你需要这样的东西:
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的攻击,并且只是假设数据库查询永远不会失败。
答案 1 :(得分:0)
您在帖子中访问了name
,但它应该是您在表单代码中提供的title
$title=$_POST['title'];this should be title
您要离开空索引,因此请将索引添加为photo
$pic=($_FILES['photo']['name']); you have not added any index here.