View Full Version : Form to Database to Email with .csv attachment

12-22-2006, 06:08 AM
Hey all!

Perhaps someone could help me. I'm attempting to have a form that upon submission does a couple things. First, it updates a database with the inputted information, then it sends an email with an attachment of the newly updated database in a csv file. I would presume this to be simple enough, but I would presume wrongly. I've got it to the point of submitting the information to the database, but I am not sure how to export the database to a file, then attach the file to an email. My code is below, perhaps someone could be of assistance?


$con = mysql_connect("localhost","username","password");
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("database", $con);

<?php include "header.html"; ?>
<p>Blah Blah</p>
<p class="telephone">Telephone ....</p>
<img src="images/b_line.gif" width="586" height="4" alt="Divider" />
<div id="maincontent">
<img src="images/business0016tb.jpg" width="177" height="271" alt="Random Stock Photo" class="floatleft" />
<p>Request email updates on class changes and additions below:</p>
<br />
$to = Name<justinsainton@gmail.com>";
$d = date('l \t\h\e dS \of F Y h:i A');
$contact = $_POST['contact'];
$title = $_POST['title'];
$email = $_POST['email'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$address1 = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$sub = "From your website - Update Page - Database Update";
$headers = "From: $company($contact) <$email>\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$mes = "<html>


<p>You've received an email from your contact page on $d. The details are as follows:


<li>Name: $name</li>
<li>Position: $position</li>
<li>Email: $email</li>
<li>Company: $company</li>
<li>Phone: $phone</li>
<li>Fax: $fax</li>
<li>Address: $add1 - $add2 - $citystatezip</li>
<li>Class Desired: $classes</li>
<li>$quantity people are planning on attending</li>
<li>Desired Location is $location</li>
<li>They <span style='text-decoration:underline; font-style: italic;'>$training</span> have a training facility available.</li>
<li>They <span style='text-decoration:underline; font-style: italic;'>$outside</span> allowing outside attendees.</li>
<li>They would like the training between $startdate and $enddate.</li>
<li>Add to update list(if empty, no)?: $update</li>
<li>Additional comments or message: $message</li>
You may reply to them directly through this email


if (empty($contact) || empty($email) || empty($company) || empty($phone) )
echo " <p>Sorry, you need to fill in all required fields.</p>";
elseif(!ereg("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
print " <p>Sorry the email address you entered looks like it's invalid.</p>";
{ $sql="INSERT INTO updates (company, contact, phone, email, address, address2, city, state, zip)

if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
print "Thank you for emailing us. We'll respond soon if necessary.";
mail($to, $sub, $mes, $headers);

<div id="footer">
<p>Site Designed by <a href="http://www.zaowebdesign.com">Zao Web Design, LLC</a></p>
<p><a href="www.xhtml.com">xhtml</a> - <a href="http://www.css.com">css</a></p>
<br class="clearer" />

12-22-2006, 07:03 AM
Creation of the CSV file should be handled using PHP's fancy CSV function fputcsv() (http://us3.php.net/manual/en/function.fputcsv.php). You can do a simple query to pull out the info you need, then use fputcsv() to write it to a file.

I've never had to create an email with an attachment from PHP so I don't know how that is done (though I do know it is possible).

12-22-2006, 08:56 AM
Honestly, that just gets me even more lost. I have no idea how to make that function work properly, uploading the files and loading them is getting me nowhere. I think what I could do is have the form send an email with a link to a php file, that upon clicking, would cause the SQL info to be exported to a csv file and subsequently downloaded. Would that be possible?

12-23-2006, 10:13 AM
Sure that's a fine way to do it-- but you'll still want to use the fputcsv() function. It's really quite simple:

//fetch data from database
$query = "SELECT firstname, lastname FROM info_tbl WHERE info_id = $workID";
$result = mysql_query($query);
if (!$result) {
die('SQL error. '.mysql_error());

for ($i = 0; $i < mysql_num_rows($result); $i++) {
$infoArray[$i] = mysql_fetch_assoc($result);

//now open a new CSV file in write mode
$csvFile = fopen('thefile.csv', 'w');

//now loop through the info array created from the query and write one line in the csv file for every row fetched from the database
foreach ($infoArray as $val) {
fputcsv($csvFile, $val);