我的php代码有问题。我正在尝试使用表单将数据写入mysql。表单还包含视频上传,该视频上传在本地存储视频并将视频的URL存储到数据库中。问题是当我测试它时会出现'videoname','videoupload','videodescription'的未定义索引的多个错误。有趣的是,如果我没有选择要上传的文件并仍在其他字段中输入内容,则会将信息写入数据库并且不会出现错误。所以它与视频有关。有人知道它可能是什么吗?谢谢! 表格代码:
<form action="videoUpload.php" id="videoUp" method='POST' enctype="multipart/form-data">
<p>Name:<textarea name="videoname" value="" class="name" ></textarea></p>
<p>Upload video:<input type="hidden" name="MAX_FILE_SIZE" value="10485760"> <input type="file" name="videoupload"> </p>
<p>Video Description:<textarea name="videodescription" value="" class="step" ></textarea></p>
<p><input type="submit" name="videosubmit" value="Submit Video" class="submit" /></p>
</form>
和php:
<?php
session_start();
//This is the directory where images will be saved
$target = "assets/video/";
$target = $target . basename( $_FILES['videoupload']['name']);
$name = $_POST['videoname'];
$description = $_POST['videodescription'];
$connect = mysql_connect("localhost","root","") or die("Couldn't connect");
mysql_select_db("fyp") or die("Couldn't find db");
$queryreg = mysql_query("INSERT INTO videos(VideoName,VideoLocation,VideoDescription) VALUES('$name','$target','$description')");
//Writes the photo to the server
if(move_uploaded_file($_FILES['videoupload']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file 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.";
}
?>
答案 0 :(得分:3)
首先,检查文件上传的error code:
$_FILES['videoupload']['error']
然后编写错误和成功方案的代码。
有关未定义索引的错误告诉您,您尝试访问的POST值未定义。 <form>
一定有问题。使用您喜欢的浏览器或Fiddler的开发人员工具来分析实际的HTTP请求,并确保传递这些值。
将行插入表中没有什么好笑的:没有实际值的验证。无论声明的变量与否,都会执行查询。未分配的变量不会导致PHP出错。
一般来说,您的代码在各个地方都缺乏验证:
mysql_query()