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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Form to Database to Email with .csv attachment

    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 Code:
    <?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-Typetext/htmlcharset=iso-8859-1n";
    $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:underlinefont-styleitalic;'>$training</span> have a training facility available.</li>
                <li>They <span style='
    text-decoration:underlinefont-styleitalic;'>$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 (companycontactphoneemailaddressaddress2citystatezip)
    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.  Well 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>
    Last edited by JustinSainton; 12-22-2006 at 05:09 AM. Reason: Privacy issues

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Creation of the CSV file should be handled using PHP's fancy CSV function fputcsv(). 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).

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Sure that's a fine way to do it-- but you'll still want to use the fputcsv() function. It's really quite simple:

    PHP Code:
    //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); 


  •  

    Posting Permissions

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