Hi,
This is my first attempt at an independent PHP program , so bear with me..
This contribution is intended to send customers an email containing log in infomation that can be used with software they download automatically upon checkout.
I believe i am done writting the code for the core of the program (Quoted below) , but i have a few questions:
1- How would i link this file to the checkout process ? (I.e How would i have oscommerce excute this php script upon checkout_sucess.php is validated)
2-I need the checkout_sucess script to pass the orderID$ to this php script, how would i go about that ?
3-SQL commands in the readcodes() function does not seem to be working well. It does seem to be outputing any variable i request. Can any one possibly tell me what i'm doing wrong ?
Below is the code for this contirbution, if any one spots any syntax errors (i'm sure there's plenty), i'd also be grateful for input
Thank you very much for your input and time
And of course feel free to use the code your self !
<?php
// To do
// Fix validate order function -- DONE
// Fix get log -- DONE
// Fix email log -- DONE
global $orderID ;
// Set debug vairable to false
$completed=FALSE;
if (validateorder($orderID)){ // Make sure order is valid before we proceed
chooseproduct($productID);
readcodes($DB_Table);
sendcodes($userID,$password,$emailFile,$customerEmail,$productName,$customerName);
$completed = TRUE; // Debug variable set to true once we finish everything successfully.
}
// if order is invalid reject request or file is accessed manually
else{
print <<<HERE
<h1> Invalid order info</h1>
<p> Please submit valid order info or order to recieve login information </p>
HERE;
die;
}
function validateorder($orderid)
{
global $customerEmail, $productID, $productName,$customerName;
// Connect to OSC DB
$conn=mysql_connect("localhost","x","x");
if (!mysql_select_db("x", $conn)){
print "Unable to connect to DB please try again later - osc connection";
return FALSE;
}
// Validate order and get productID
$sql = "SELECT * FROM x WHERE orders_id = '$orderID'";
$result= mysql_query($sql,$conn);
if (result == FALSE){
return FALSE;
}
else{
$orderdetails= mysql_fetch_assoc($result);
$productID = $orderdetails["products_id"];
$productName =$orderdetails["products_name"];
}
// Now connect to Orders table to get customer email
$sql = "SELECT * FROM x WHERE orders_id = '$orderID'";
$result= mysql_query($sql,$conn);
$details= mysql_fetch_assoc($result);
$customerEmail=$details["customers_email_address"];
$customerName=$details["customers_name"];
return TRUE; // Order does exist
}
function readcodes($DB_Table)
{
global $userID, $password;
//Connect to DB
$conn=mysql_connect("localhost","x","x");
mysql_select_db("x", $conn);
/*
if (){
print "Unable to connect to DB please try again later - unlock connection";
die;
}
*/
$sql = "SELECT * FROM ". $DB_Table. " WHERE used = 0";
$result = mysql_query($sql,$conn);
// Get one log row
$row = mysql_fetch_assoc($result);
list($logNumber,$userID,$password,$used,$order,$completed)= $row;
// Update DB log used
$sql=<<<HERE
UPDATE x
SET
used='1'
order='$orderid'
completed='1'
WHERE id='$lognumber'
HERE;
mysql_query($sql,$conn); // Do update
}
function chooseproduct($productID)
{
global $emailFile,$DB_Table;
switch($productID){
case 32:
$emailFile="x.dat";
$DB_Table="x";
break;
case 34:
$emailFile="y.dat";
$DB_Table="y";
break;
case 35:
$emailFile="z.dat";
$DB_Table="z";
break;
default:
print "Invalid Product Id passed";
die;
}
}
function sendcodes($userID,$password,$emailFile,$customerEmail,$productName,$customerName)
{
// Get product email and Replace fields with data
$email = file($emailFile);
foreach ($email as $currentline){
$currentline = str_replace("***||customerName||***",$customerName,$currentline);
$currentline = str_replace("***||userID||***",$usedID,$currentline);
$currentline = str_replace("***||password||***",$usedID,$currentline);
$productEmail .= rtrim($currentline) . "<br>\n";
}
// Finally email the customer his instructions
// mail function structure(customer email,subject,message,head,para)
mail($customerEmail,$productTitle,$productEmail);
// fclose($fp);
}
?>