使用php表单从mysql下载文件时遇到问题

使用php表单从mysql下载文件时遇到问题,php,mysql,download,Php,Mysql,Download,嗨,我刚刚开发了一个原型,允许用户通过动态下拉菜单选择一些选项,然后上传一个小文件到mysql。我刚刚完成了搜索功能,该功能运行良好,并正确返回结果。在过去的几天里,让我陷入困境的是允许用户下载搜索结果中返回的一个文件。我把结果放在一个html表格中,并使用一个表单作为下载选项,这就是我怀疑表单存在问题的地方,因为当我把文件下载弄得一团糟时,下载是有效的,但当用户点击上一页的搜索时 我的阅读让我走了这么远,我看过一些问题,但似乎没有一个能起作用。我还是php新手,所以请原谅我混乱的结构。任何帮助

嗨,我刚刚开发了一个原型,允许用户通过动态下拉菜单选择一些选项,然后上传一个小文件到mysql。我刚刚完成了搜索功能,该功能运行良好,并正确返回结果。在过去的几天里,让我陷入困境的是允许用户下载搜索结果中返回的一个文件。我把结果放在一个html表格中,并使用一个表单作为下载选项,这就是我怀疑表单存在问题的地方,因为当我把文件下载弄得一团糟时,下载是有效的,但当用户点击上一页的搜索时

我的阅读让我走了这么远,我看过一些问题,但似乎没有一个能起作用。我还是php新手,所以请原谅我混乱的结构。任何帮助都将不胜感激

下面是results_page.php的脚本

 if (isset ($resultts) and ($tests))

{



$sql = "SELECT p.logID, p.fileData, p.fileName, p.mimeType, p.dateCreated, t.testName,      r.resultType
    FROM result AS r 
    INNER JOIN product_logs AS p 
    ON r.resultID=p.resultID
    INNER JOIN test AS t
    ON t.testID=r.testID
    WHERE t.testName LIKE '%$tests%'
    AND r.resultType LIKE '%$resultts%'
    ORDER BY p.dateCreated DESC";

$result1= mysqli_query($sql) or die (mysqli_error());



while ($row = mysqli_fetch_array($result1))
{
$filename = $row['fileName'];
$filetype = $row['mimeType'];
$filetest = $row['testName'];
$fileresult= $row['resultType'];
$filedate=$row['dateCreated'];
$fileid=$row['logID'];
$filedata=$row['fileData'];

echo"<table>";
echo"<tr><th>File Name</th><th>File Type</th><th>Test</th><th>Result</th><th>Date</th>    <th>options</th></tr>";
echo"<tr valign=top>";
echo "</tr>";
echo"<td><'".$filename."'</td>";
echo "<td><'".$filetype."'</td>";
echo"<td><'".$filetest."'</td>";
echo"<td><'".$fileresult."'</td>";
echo"<td><'".$filedate."'</td>";
echo "<td><form action= confirm_download.php method= post</td>";
echo "<div>";
echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";
echo "<div>";
echo "</form>";
echo "</td>";

echo "</table>";
}





if ($_POST['action']=='download')
{
// Content-type must come before Content-disposition
header("Content-type: $filetype");
header("Content-disposition: attachment filename=$filename");
header('Content-length: ' . strlen($filedata));


echo $filedata;
exit();
}



}   
if(isset($resultts)和($tests))
{
$sql=“选择p.logID、p.fileData、p.fileName、p.mimeType、p.dateCreated、t.testName、r.resultType
从结果中选择r
内部联接产品_记录为p
关于r.resultID=p.resultID
内部连接测试为t
关于t.testID=r.testID
其中t.testName类似“%$tests%”
和r.resultType类似“%$resultts%”
按p.dateCreated DESC”订购;
$result1=mysqli_query($sql)或die(mysqli_error());
while($row=mysqli\u fetch\u数组($result1))
{
$filename=$row['filename'];
$filetype=$row['mimeType'];
$filetest=$row['testName'];
$fileresult=$row['resultType'];
$filedate=$row['dateCreated'];
$fileid=$row['logID'];
$filedata=$row['filedata'];
回声“;
echo“文件名文件类型测试结果日期选项”;
回声“;
回声“;

echo“您有两个名为action的输入。从提交按钮中删除name=action

echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";
应该变成这个

echo "<form action= confirm_download.php method= post>";
echo "<div>";
echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";
echo "</div>";
echo "</form>";
这永远不会是真的(除非文件名实际上是“下载”…) 您可以将if更改为:

if (isset($_POST['action']))

数据库不用于存储文件。请将文件放在磁盘上,并仅存储对数据库中文件的引用。您的$filename包含哪些内容?完整的文件存储路径以及文件名或仅文件名。您到底有什么问题。您是否收到任何错误消息?我得到的索引操作是空的ID您注意到您有
感谢您的响应。我发出了很遗憾,错误仍然显示,没有下载任何文件。你认为是if($\u POST['action']='download')还是headersI调整了表单,它只是将浏览器发送到confirm_download.php,我删除了,if($\u POST['action']='download'))当在results_page.php之前的页面中点击搜索按钮时,匹配搜索条件的文件将下载到浏览器中。这是我的目标,但在下一页中,只有当用户点击下载时,才进行搜索。感谢您的回复。我猜当用户从表单中选择下载,然后选择标题。抱歉,我不明白你在说什么。首先,我的建议有效吗?未定义的索引错误消失了吗?现在有新错误吗?抱歉,只是大声思考。没有未定义的索引。
echo "<form action= confirm_download.php method= post>";
echo "<div>";
echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";
echo "</div>";
echo "</form>";
echo "<input type =hidden name=action value= '".$filename."' />";
if ($_POST['action']=='download')
if (isset($_POST['action']))