I am off to bed too…Here is the complete code:
[php]
<?php
// This is my Insert to Database Code //
define('DB_NAME', 'database');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
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['name1'];
$value3 = $_POST['name2'];
$value4 = $_POST['address'];
$value5 = $_POST['city'];
$value6 = $_POST['state'];
$value7 = $_POST['zip'];
$value8 = $_POST['homephone'];
$value9 = $_POST['cellphone'];
$value10 = $_POST['email'];
$value11 = $_POST['age'];
$value12 = $_POST['maritalstatus'];
$value13 = $_POST['income'];
$value14 = $_POST['contact1'];
$value15 = $_POST['contact2'];
$value16 = $_POST['contact3'];
$value17 = $_POST['date1'];
$value18 = $_POST['date2'];
$value19 = $_POST['date3'];
$value20 = $_POST['gift'];
$sql = "INSERT INTO clients (groupname, name1, name2, address, city, state, zip, homephone, cellphone, email, age, maritalstatus, income, contact1, contact2, contact3, date1, date2, date3, gift) VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18', '$value19', '$value20')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
// This is my Excel File Creation Code //
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$sql = "SELECT
`groupname` AS `Group`,
`name1` AS `Customer Name 1`,
`name2` AS `Customer Name 2`,
`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`,
`gift` AS `Gift Choice`
FROM fundtour_info.clients clients";
// Query Database
$result=mysql_query($sql);
$filename = 'file.xls';
// XLS Data Cell
ob_start(); // start the file
xlsBOF();
xlsWriteString(0,0,"Group");
xlsWriteString(0,1,"Name 1");
xlsWriteString(0,2,"Name 2");
xlsWriteString(0,3,"Address");
xlsWriteString(0,4,"City");
xlsWriteString(0,5,"State");
xlsWriteString(0,6,"Zip Code");
xlsWriteString(0,7,"Home Phone");
xlsWriteString(0,8,"Cell Phone");
xlsWriteString(0,9,"E-mail Address :");
xlsWriteString(0,10,"Age Group");
xlsWriteString(0,11,"Marital Status");
xlsWriteString(0,12,"Income");
xlsWriteString(0,13,"Contact Via");
xlsWriteString(0,14,"Dates");
xlsWriteString(0,15,"Gift Choice");
$xlsRow = 2;
while(list($groupname, $name1, $name2, $address, $city, $state, $zip, $homephone, $cellphone, $email, $age, $maritalstatus, $income, $contact1, $contact2, $contact3,$date1, $date3, $date3, $gift)=mysql_fetch_row($result)) {
++$i;
xlsWriteString($xlsRow,0,"$groupname");
xlsWriteString($xlsRow,1,"$name1");
xlsWriteString($xlsRow,2,"$name2");
xlsWriteString($xlsRow,3,"$address");
xlsWriteString($xlsRow,4,"$city");
xlsWriteString($xlsRow,5,"$state");
xlsWriteString($xlsRow,6,"$zip");
xlsWriteString($xlsRow,7,"$homephone");
xlsWriteString($xlsRow,8,"$cellphone");
xlsWriteString($xlsRow,9,"$email");
xlsWriteString($xlsRow,10,"$age");
xlsWriteString($xlsRow,11,"$maritalstatus");
xlsWriteString($xlsRow,12,"$income");
xlsWriteString($xlsRow,13,"$contact1, $contact2, $contact3");
xlsWriteString($xlsRow,14,"$date1, $date3, $date3");
xlsWriteString($xlsRow,15,"$gift");
$xlsRow++;
}
xlsEOF(); // $filename should be set to some writeable location
file_put_contents($filename, ob_get_clean());
exit();
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteString($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
// This is my Mail Code //
include("class.phpmailer.php");
include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = "
[email protected]"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->From = "
[email protected]";
$mail->FromName = "Site Info";
$mail->Subject = "New Client Signup";
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddReplyTo("
[email protected]");
$mail->AddAttachment("file.xls"); // attachment
$mail->AddAddress("
[email protected]");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
[/php]