Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:52:59+00:00 2026-06-10T22:52:59+00:00

The problem with this code is that each time I export from MySQL to

  • 0

The problem with this code is that each time I export from MySQL to csv it overwrites the already created csv. I want this code to check if there is an already created csv file -> create new one. Ex. (Sales-2012-9-8.csv) to (Sales1-2012-9-8.csv)…This code works great on exporting records from MySQL..But when it comes to export another csv it overwrites the current csv file name.

Oh yea, one more thing..How is it possible to add Table Headers at the first row of csv file. Ex ( Item No – Qty – Selling Price – Date )?

Thank You

<?php
require_once('connect_db.php');
$fdate = $_POST['fdate'];
$tdate = $_POST['tdate'];

$result = mysql_query("SELECT item_no, qty, discount_price, date FROM sold_items WHERE date BETWEEN '".$fdate."' AND '".$tdate."' ORDER BY date Desc");

$filename = $name = "Sales";
$index = 1;
while(file_exists($filename)) {
  $filename = $name.$index;
  $index++;
  echo "File Already Exists";
}

if(!$f = fopen("E:/{$filename}-{$tdate}.csv", 'w')){
    ?>
    <script language="javascript">
    alert('USB FLASH NOT INSERTED');
    history.back();
    </script>
    <?php
}

while($row = mysql_fetch_array($result, MYSQL_NUM))
{
    fputcsv($f, $row);
}

fclose($f);


?>
<center><font style="font-size:28px; font-weight:bold; color:#C00;">USB Transfer Successful</font></center> 
<center><input type="button" onclick="javascript:window.close()" value="Close Window" /></center>
<?php

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:53:00+00:00Added an answer on June 10, 2026 at 10:53 pm

    Try this variant. I think file_exists() function see into other folder than “E:/”:

    $tdate = date();
    $filename = $name = "Sales";
    $index = 1;
    while(file_exists("E:/{$filename}-{$tdate}.csv")) {
      $filename = $name.$index;
      $index++;
      echo "File {$filename}-{$tdate}.csv Already Exists";
    }
    
    if(!$f = fopen("E:/{$filename}-{$tdate}.csv", 'w')){
        ?>
        <script language="javascript">
        alert('USB FLASH NOT INSERTED');
        history.back();
        </script>
        <?php
    }
    

    As for the second question:

    Oh yea, one more thing..How is it possible to add Table Headers at the first row of csv file. Ex ( Item No – Qty – Selling Price – Date )?

    As I know, the first line of a csv-file is recognized as headers. So, try write your headers before writing the query results. E.g.

    fputcsv($f, array('Item No', 'Qty', 'Selling Price', 'Date'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Problem I'm receiving crash reports from users that look like this: Code Type:
Im having a bit of a problem with this code each time i execute
I got this code today that works for jQuery v7. The problem is that
hi i'm having a problem with this code. my problem is that the first
I am trying to solve this exponentiation problem . Here is the code that
Here is a JSFiddle that demonstrates the problem... http://jsfiddle.net/L2NBP/5/ For obvious reasons this code
This code is a linked list implementation of a stack that solves postfix problems
i am a beginner and i have a problem : this code doesnt compile
So my next problem with this code. It seemse to not be finding a
I have a problem in this code when I enter a string, instead of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.