0

I have a mysql database and I would like to search with a query - catalogue name (catn) - and then download the whole row associate with that query. I have wrote a php code but nothing is writing to the file. The download starts after clicking on the Download button but the file is empty. Could anyone please help me with this?

I have two php files.

The first one has the code for the button:

<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>

The second file - download.php - has the code for the downloading. I have omitted the part of code regarding the connection to 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]);
}
?>

Solution

I have managed to make it work with the help of the comments (thank you!) and here is the code:

<?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]);
    }
    ?>
myrtle
  • 19
  • 2
  • And the problem is...? – pavel Mar 22 '18 at 16:50
  • mysql_* is deprecated and is removed in PHP 7. Use mysqli_* or prepared statements. – jonas3344 Mar 22 '18 at 16:51
  • @panther it downloads an empty file. – myrtle Mar 22 '18 at 16:52
  • @jonas3344 I am using PHP 5. – myrtle Mar 22 '18 at 16:54
  • Yeah, doesn't matter, it's deprecated already there and it's insecure. If you want to find out what's wrong with your script you have to debug. Check if the query is working, check if you get a result. https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – jonas3344 Mar 22 '18 at 16:57
  • You're not outputting the contents of the file to the browser. Add something like `readfile('file.csv');` after you output the headers. Or skip the file entirely (your current method will lead to problems if you have multiple concurrent users): move the headers above your while loop and simply `echo $row['catn'];`. – rickdenhaan Mar 22 '18 at 16:58
  • @jonas3344 thank you but I was told I have to use the mysql_* format. – myrtle Mar 24 '18 at 10:38
  • @rickdenhaan thank you. It still does not work. I think the main problem is that the query supplied by the user is not getting searched. – myrtle Mar 24 '18 at 10:40
  • @myrtle should be easy enough to debug that. Output `$query` and see if you get a result if you execute that query manually using something like phpMyAdmin. Check if `$sqlsearch` is `false` (indicates that an error occurred, you can then use `mysql_error()` to find out what the error is). Check if `mysql_num_rows($sqlsearch)` indicates that there are any matching rows. Etc. – rickdenhaan Mar 24 '18 at 13:59
  • @rickdenhaan thank you so much. I was able to debug it and it works now. – myrtle Mar 25 '18 at 12:00

0 Answers0