what I am trying to do is use this php script to load the data being submitted in the html form into my database and then populate the database into an excel (csv) file and then e-mail it to my address.

Everything works great it populates into the database and creates the xls file perfect. But it is wanting me to download the file. What can I add to the script to have it e-mail the file to my e-mail address INSTEAD of downloading it.

HERE IS THE CODE:

<?php

 header( 'Content-Type: text/csv' );
 header( 'Content-Disposition: attachment;filename=Fund_Tours_Client_List.csv' );

define('DB_NAME', 'mydatabase');
define('DB_USER', 'myusername');
define('DB_PASSWORD', 'mypassword');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if (!$link)   
            {
                die('Could not connect: ' . mysql_error());  
                }   

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$value1 = $_POST['groupname'];
$value2 = $_POST['name'];
$value3 = $_POST['address'];
$value4 = $_POST['city'];
$value5 = $_POST['state'];
$value6 = $_POST['zip'];
$value7 = $_POST['homephone'];
$value8 = $_POST['cellphone'];
$value9 = $_POST['email'];
$value10 = $_POST['age'];
$value11 = $_POST['maritalstatus'];
$value12 = $_POST['income'];
$value13 = $_POST['contact1'];
$value14 = $_POST['contact2'];
$value15 = $_POST['contact3'];
$value16 = $_POST['date1'];
$value17 = $_POST['date2'];
$value18 = $_POST['date3'];


$sql = "INSERT INTO clients (groupname, name, address, city, state, zip, homephone, cellphone, email, age, maritalstatus, income, contact1, contact2, contact3, date1, date2, date3) VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18')";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}

mysql_close();

$conn = mysql_connect( 'localhost', 'fundtour_rick', 'Fund.Tours' ) or die( mysql_error( ) );
 mysql_select_db( 'fundtour_info', $conn ) or die( mysql_error( $conn ) );

 $query = sprintf( 'SELECT
`groupname` AS `Group`,
`name` AS `Customer Name`, 
`address` AS `Address`,
`city` AS `City`,
`state` AS `State`,
`zip` AS `Zip Code`,
`homephone` AS `Home Phone`,
`cellphone` AS `Cell Phone`,
`email` AS `E-Mail`,
`age` AS `Age Group`,
`maritalstatus` AS `Marital Status`,
`income` AS `Household Income`,
`contact1` AS `Contact VIA`,
`contact2` AS `Contact VIA`,
`contact3` AS `Contact VIA`,
`date1` AS `1st Date`,
`date2` AS `2nd Date`,
`date3` AS `3rd Date`

FROM fundtour_info.clients clients' );

 $result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );

 $row = mysql_fetch_assoc( $result );
 if ( $row )
 {
 echocsv( array_keys( $row ) );
 }

 while ( $row )
 {
 echocsv( $row );
 $row = mysql_fetch_assoc( $result );
 }

 function echocsv( $fields )
 {
 $separator = '';
 foreach ( $fields as $field )
 {
 if ( preg_match( '/\\r|\\n|,|"/', $field ) )
 {
 $field = '"' . str_replace( '"', '""', $field ) . '"';
 }
 echo $separator . $field;
 $separator = ',';
 }
 echo "\r\n";
 }

?>

Thanks for any help

Recommended Answers

what I am trying to do is use this php script to load the data being submitted in the html form into my database and then populate the database into an excel (csv) file and then e-mail it to my address.

I think someone ask the question here:

Jump to Post

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.