I'm trying to create a script that when a "new user" submits a form and there info is added to the database it emails the "agent" that is associated with the "access code" the new user submitted. right now I have it automatically send an email to the user but I dont know how to make it send to the associated agent. The agents and the users are on different tables. So far my code looks like this:
<?
$DBhost = "localhost";
$DBuser = "username";
$DBpass = "password";
$db_name = "database_name";
$login_table = "user";
$connection = @mysql_connect($DBhost,$DBuser,$DBpass) or die("Couldn't Connect.");
$db = @mysql_select_db($db_name, $connection) or die("Sorry, I could not select the requested Database ");
if ( isset( $first_name ) && isset( $last_name ) && isset( $phone ) && isset( $product )&& isset( $email )&& isset( $access_code) ) {
$dberror = "You have not submitted all the needed data";
$ret = add_to_database( $first_name, $last_name, $phone, $product, $email, $access_code, $dberror );
if ( ! $ret )
print "ERROR: $dberror<br>";
else
$reply= "thank you for your patience";
$response_mail = "Hello $first_name $last_name\n I wanted to personally thank you for viewing our presentation. An agent in your area will be contacting you soon to discuss a $product that is right for you. Again thank you for taking the time to check us out.";
$header = "From: email_address";
$subject = "Thank You for Viewing Our Presentation";
mail("$email","$subject","$response_mail","$header ");
} else {
write_form();
}
function add_to_database( $first_name, $last_name, $phone, $product, $email, $access_code ) {
$link = mysql_pconnect( "localhost","$DBuser","$DBpass" );
if ( ! $link ) {
$dberror = "Couldn't connect to Database";
return false;
}
if ( ! mysql_select_db( $db, $link )){
$dberror = mysql_error();
return false;
}
$query= "INSERT INTO user ( first_name, last_name, phone, product, email, access_code )
values( '$first_name', '$last_name', '$phone', '$product', '$email', '$access_code' )";
if ( ! mysql_query( $query, $link ) ) {
$dberror + mysql_error();
return false;
}
return true;
}
?>
I'm not exactly sure as to where to start, any help would be greatly appreciated. except I know I'd have to run a query like:
"SELECT email FROM $login_table WHERE access_code = \"$access_code\"";
but from there I get confused about how it should be written.
-AaronN