我有一个mysql数据库,我想用查询 - 目录名称(catn)进行搜索 - 然后下载与该查询关联的整行。我写了一个PHP代码,但没有写入该文件。单击“下载”按钮后开始下载,但文件为空。有人可以帮我这个吗?
我有两个php文件。
第一个包含按钮的代码:
<html>
<body>
<form action="download.php" method="post"> <pre> Catalogue number <span class="inner-pre"> <input name="catnq" input type="text";/>
<input type="submit" value="Download";/>
</pre></form>
</body>
</html>
第二个文件 - download.php - 有下载代码。我省略了与mysql连接的代码部分。
<?php
if (($_POST['catnq'] != "")) {
$fp = fopen('file.csv', 'w');
$query = "select * from Compounds where catn =";
$query = $query." '".get_post('catnq')."'";
$sqlsearch = mysql_query($query);
while($row = mysql_fetch_array($sqlsearch)){
$name = $row['catn'];
fwrite($fp, $name);
}
fclose($fp);
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
}
function get_post($var){
return mysql_real_escape_string($_POST[$var]);
}
?>
解决方案
我已经设法在评论的帮助下工作(谢谢!),这里是代码:
<?php
$input_user = $_POST["catnq"];
if (($_POST['catnq'] != "")) {
$fp = fopen('file.csv', 'w');
$query = "select * from Compounds where catn = '$input_user' ";
$sqlsearch = mysql_query($query);
while($row = mysql_fetch_array($sqlsearch)){
$name = $row['catn'];
echo $name;
echo "\n";
}
readfile('file.csv');
fclose($fp);
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
}
function get_post($var){
return mysql_real_escape_string($_POST[$var]);
}
?>