...

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



JustinSainton
12-22-2006, 05: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?



<?php

$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 />
<?php
$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>


<body>

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

<ul>

<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>
</ul>
You may reply to them directly through this email
</p>

</body>

</html>";
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>";
}
else
{ $sql="INSERT INTO updates (company, contact, phone, email, address, address2, city, state, zip)
VALUES
('$company','$contact','$phone','$email','$address','$address2','$city','$state','$zip')";

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

?></div>
<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>
</div>
<br class="clearer" />
</div>
</body>
</html>

Fumigator
12-22-2006, 06: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).

JustinSainton
12-22-2006, 07: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?

Fumigator
12-23-2006, 09: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);
}

fclose($csvFile);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum