Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Las Vegas
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Export to CSV contains date validation Javascript

    I have a page that exports data to a csv file. However I get about 95 lines of a DHTML date validation script by Smartwebby.

    Its hosted on dreamhost, and the server is on PHP 5.2 FAST CGI and MySQL 5 database.

    Here is the php code I got from a tutorial:

    Code:
    $sql = "SELECT * FROM `TABLE`";
    
    
        $csv_terminated = "\n";
        $csv_separator = ",";
        $csv_enclosed = '"';
        $csv_escaped = "\\";
        $filename = 'export.csv';
    
     
        // Gets the data from the database
        $result = mysql_query($sql, $DATABASE) or die(mysql_error());
        $fields_cnt = mysql_num_fields($result);
     
     
        $schema_insert = '';
     
        for ($i = 0; $i < $fields_cnt; $i++)
        {
            $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
                stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
            $schema_insert .= $l;
            $schema_insert .= $csv_separator;
        } // end for
     
        $out = trim(substr($schema_insert, 0, -1));
        $out .= $csv_terminated;
     
        // Format the data
        while ($row = mysql_fetch_array($result))
        {
            $schema_insert = '';
            for ($j = 0; $j < $fields_cnt; $j++)
            {
                if ($row[$j] == '0' || $row[$j] != '')
                {
     
                    if ($csv_enclosed == '')
                    {
                        $schema_insert .= $row[$j];
                    } else
                    {
                        $schema_insert .= $csv_enclosed . 
    					str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                    }
                } else
                {
                    $schema_insert .= '';
                }
     
                if ($j < $fields_cnt - 1)
                {
                    $schema_insert .= $csv_separator;
                }
            } // end for
     
            $out .= $schema_insert;
            $out .= $csv_terminated;
        } // end while
     
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Length: " . strlen($out));
        // Output to browser with appropriate mime type, you choose ;)
        header("Content-type: text/x-csv");
        //header("Content-type: text/csv");
        //header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=$filename");
        echo $out;

    Any ideas as to why I would get it on the export?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    It's not in the source you posted, so it's likely in something you omitted or your data source.

  • #3
    New Coder
    Join Date
    May 2009
    Location
    Las Vegas
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I can export from phpmyadmin fine, so it must be something else in the page, or just a MySQL error that I cannot find.


    I have been eliminating code to narrow it down, but I am pretty bare bones right now. Here is the full source,

    Code:
    <?php require_once('/Connections/database.php'); ?>
    <?php
    
    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "sqlque")) {
    
    //Creating sql query
    mysql_select_db($database_one, $database);
    $sql = "SELECT * FROM `TABLE`";
    // Export script
    	$csv_terminated = "\n";
        $csv_separator = ",";
        $csv_enclosed = '"';
        $csv_escaped = "\\";
    	$filename = 'export.csv';
     
        // Gets the data from the database
        $result = mysql_query($sql, $database) or die(mysql_error());
        $fields_cnt = mysql_num_fields($result);
     
        $schema_insert = '';
     
        for ($i = 0; $i < $fields_cnt; $i++)
        {
            $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
                stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
            $schema_insert .= $l;
            $schema_insert .= $csv_separator;
        } // end for
     
        $out = trim(substr($schema_insert, 0, -1));
        $out .= $csv_terminated;
     
        // Format the data
        while ($row = mysql_fetch_array($result))
        {
            $schema_insert = '';
            for ($j = 0; $j < $fields_cnt; $j++)
            {
                if ($row[$j] == '0' || $row[$j] != '')
                {
     
                    if ($csv_enclosed == '')
                    {
                        $schema_insert .= $row[$j];
                    } else
                    {
                        $schema_insert .= $csv_enclosed . 
    					str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                    }
                } else
                {
                    $schema_insert .= '';
                }
     
                if ($j < $fields_cnt - 1)
                {
                    $schema_insert .= $csv_separator;
                }
            } // end for
     
            $out .= $schema_insert;
            $out .= $csv_terminated;
        } // end while
     
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Length: " . strlen($out));
        // Output to browser with appropriate mime type, you choose ;)
        header("Content-type: text/x-csv");
        //header("Content-type: text/csv");
        //header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=$filename");
        echo $out;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    		<title>Page title</title>
    		<link href="../styles/screen.css" rel="stylesheet" type="text/css" />
    	</head>
    	<body>
    		<div id="content">
    			<h1>&nbsp;</h1>
          <h3><a href="../index.php">Home Page</a>&nbsp;|&nbsp;<a href="../contact.php">Contact</a>&nbsp;|&nbsp;<a href="monthly.php">Monthly Report</a></h3>
    			<form action="<?php echo $editFormAction; ?>" method="POST" name="form">
                    <p>&nbsp;</p>
    				<fieldset id="Page E">
    					<legend><span>Treatment Service</span></legend>
    				</fieldset>
    				<fieldset id="submitform">
    			      <p>
    			        <input type="submit" name="Submit" id="formsubmit" value="Download" />
    			        </label>
    			      </p>
    				</fieldset>
    				<input type="hidden" name="MM_update" value="sqlque" />
              </form>
    	<p>&nbsp;</p>
    </div>
    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •