0

I'm currently trying to figure out how I would be able to export the table result done by the search.

<!DOCTYPE html>

<html>
<head>
    <script type="text/javascript" src="http://gc.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=XeIP$
        <script src="js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
        <title>AJAX Persons Example - Displaying FILES</title>
        <script>
        $(document).ready(function() {
                $( "#AthleteName" ).change(function() {

                        $.ajax({
                                url: 'searchPlayer.php',
                                data: {searchName: $( "#AthleteName" ).val()},
                                success: function(data){
                                        $('#Nameresult').html(data);

                                }
                        });
                });

        });
        </script>
</head>
<body>
        <h3>Olypmic Dataset</h3>

        <input class="xlarge" id="AthleteName" type="search" size="30" placeholder="sid contains"/>

        <div id="Nameresult">Search Result</div>

        <br/><br/>

</body>
</html>

Above code is my index html which will display the results, and I'm using searchPlayer.php that does the function.

<?php
        require "dbutil.php";
        $db = DbUtil::loginConnection();

        $stmt = $db->stmt_init();

        if($stmt->prepare("select * from athlete where Name like ?") or die(mysqli_error($db))) {
                $searchString = '%' . $_GET['searchName'] . '%';
                $stmt->bind_param(s, $searchString);
                $stmt->execute();
                $stmt->bind_result($ID, $Name, $Sex, $Height, $Weight);
                $col_att = array();

                echo "<form method='post' action='download.php'><input type='submit' value='Export' name='Export'>";
                echo "<table border=1><th>ID</th><th>Name</th><th>Sex</th><th>Height</th><th>Weight</th>\n";
                while($stmt->fetch()) {
                        echo "<tr><td>$ID</td><td>$Name</td><td>$Sex</td><td>$Height</td><td>$Weight</td>";
                        echo '<td><a href="AthleteSite.php?ID='. $ID .'">View More</a></td></tr>';
                        $col_att[] = array($ID, $Name, $Sex, $Height, $Weight);
                }
                echo "</table>";
                $serialize_user_arr = serialize($col_att);
                echo "<textarea name='export_data' style='display: none;'><?php echo $serialize_user_arr; ?></textarea></form>";      $
                $stmt->close();
        }

        $db->close();


?>

However, when the search result comes and I try to export, it just creates an empty csv file. I'm not sure what's going on that could create this problem. Is the mechanism that is done on searchPlayer.php incorrect?

<?php
$filename = 'users.csv';
$export_data = unserialize($_POST['export_data']);

// file creation
$file = fopen($filename,"w");

foreach ($export_data as $line){
 fputcsv($file,$line);
}

fclose($file);

// download
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; ");

readfile($filename);

// deleting file
unlink($filename);
exit();

This download.php serves as creating the csv file.

Davin Um
  • 1
  • 1
  • Where do you actually create the CSV content? – Nigel Ren May 02 '21 at 17:56
  • @NigelRen Hi, I forgot to upload that code. I've edited so that it contains download.php – Davin Um May 02 '21 at 18:00
  • Have you checked to make sure $export_data is an array by the time it gets to the `foreach()` block? Maybe check it with a `die('
    '.print_r($export_data,1).'
    ');`
    – Kinglish May 02 '21 at 19:23
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman May 02 '21 at 19:42

0 Answers0