954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php/mysql inserting variable into subject line of email

I have two tables. tbl_clients and tbl_projects. They both contain auto incremented primary key columns.

For adding new projects to tbl_projects, I use tbl_clients' clientname field to populate a dropdown menu on the new projects html form. The user chooses a client name from the menu, and the primary key of that client is inserted into tbl_projects for relational purposes.

Next, I have a php script which accepts this form data and inserts it into tbl_projects and then emails the data to a recipient. This is working well except for the client name.

Since what is being passed to the page is the primary key and not the client name, how do i translate the key into it's corresponding client name so i can use it as a variable for the subject line? I'm assuming some sort of query but I'm just not familiar enough with php to get it right. Is there a way to pass the client name from the previous page along with it's primary key?

//This is the php within the form that creates the client name/clientid menu
<?php
$query = "SELECT clientname , clientid FROM tbl_clients";
$result = mysql_query($query) or die(mysql_error());
$dropdown = "";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n{$row['clientname']}";}
$dropdown .= "\r\n";
echo $dropdown;
?>

//This is the script that inserts the data to mysql and emails it.
<?php
mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxx");?>

<?php

$clientid=$_POST['clientid'];
$project=$_POST['project'];
$type=$_POST['type'];
$target=$_POST ['target'];
$priority=$_POST ['priority'];
$notes=$_POST['notes'];
$start=$_POST['start'];
$status=$_POST['status'];
$finish=$_POST['finish'];
$time=$_POST['time'];
$perhour=$_POST['perhour'];
$invoice=$_POST['invoice'];

$sql="INSERT INTO tbl_projects (clientid , project , type , target , priority , notes , start , status , finish , time , perhour , invoice) VALUES ('$clientid' , '$project' , '$type' , '$target' , '$priority' , '$notes' , '$start' , '$status' , '$finish' , '$time' , '$perhour' , '$invoice')";

$result=mysql_query($sql);
if($result){
echo "Successful";
echo "
";
echo "Back to main page";
}

else {
echo "ERROR";
}

//MAIL

$to = "xxx@xxx.com";
$from = "xxx@xxx.com";
$subject = "$clientname"; //< this is where i'm having the trouble
$message = "the birds.";
mail($to , $subject , $from , $message) ;

//MAIL

mysql_close();

?>

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Since you pass only clientid to the mail script, you have to query tbl_clients again to get the name via the id.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

2 choices:
- write an if statement within the fetch loop that picks out the client id you want, but that's slow and ugly on a server.
- change your query to select a single row where you are selecting only clientid you want. if this is not the data you were gunning for and this is a 2-table INNER JOIN, do that.

SELECT tbl_clients.clientname AS clientname,tbl_clients.clientid AS clientid FROM tbl_clients INNER JOIN othertbl ON tbl_clients.clientid='42' AND othertbl.client_id=tbl_clients.clientid
$query = "SELECT clientname , clientid FROM tbl_clients WHERE clientid='42'";


you can replace 42 with some variable.

jmichae3
Junior Poster
106 posts since Jul 2011
Reputation Points: 14
Solved Threads: 11
 

Thanks for responding.

What code would i put here?:

$subject=

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

I'm still having trouble with this. I want the subject line of the email to be the clientname generated by the query near the bottom.

I've got the query working and echoing the proper clientname field vs it's primary key. I don't know how to get the query results in the subject line. I've tried all i know and read all i can to no avail.

Please help!

<?php

$clientid=$_POST['clientid'];
$project=$_POST['project'];
$type=$_POST['type'];
$target=$_POST ['target'];
$priority=$_POST ['priority'];
$notes=$_POST['notes'];
$start=$_POST['start'];
$status=$_POST['status'];
$finish=$_POST['finish'];
$time=$_POST['time'];
$perhour=$_POST['perhour'];
$invoice=$_POST['invoice'];

$sql="INSERT INTO tbl_projects (clientid , project , type , target , priority , notes , start , status , finish , time , perhour , invoice) VALUES ('$clientid' , '$project' , '$type' , '$target' , '$priority' , '$notes' , '$start' , '$status' , '$finish' , '$time' , '$perhour' , '$invoice')";

$result=mysql_query($sql);
if($result){
echo "Successful";
echo "
";
echo "Back to main page";
echo "
";
}

else {
echo "ERROR";
}

/*$query = "SELECT clientname , clientid FROM tbl_clients WHERE clientid= $clientid";
$result=mysql_query($query);}*/

$result = mysql_query("SELECT clientname , clientid FROM tbl_clients WHERE clientid= $clientid");
while ($row = mysql_fetch_array($result)) {

echo $row['clientname'];}

$to = "xxx@xxx.com";
$from = "xxx@xxx.com";
$subject = $row['clientname']; <-------------NOT WORKING
$message = "the birds.";
mail($to , $subject , $from , $message) ;

?>

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Do not use while, use if

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

from your first post - did you intend to have " "
$subject = "$clientname"; //< this is where i'm having the trouble

momo219
Light Poster
44 posts since Dec 2009
Reputation Points: 11
Solved Threads: 3
 

Momo, Thanks for responding, i'm not sure what i intend other than needing the clientname to appear as the subject line. I'll try it with the quotes and if rather than while. I'll let you know how it goes

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

pritaeas, the if worked! Thanks so much. If you've got a moment do you mind telling me why that worked?

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

while keeps returning an array, until there is no more, returning false (no values). The if returns the first record/array from your query only.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Good that you got it working, but just to clarify - I wasn't suggesting you use the "" around the $var. I was suggesting that you shouldn't be surrounding the $var in "" as per your first post, as this would make the $var (name) a string rather than its contents.

Irrelevant now but just clearing up :)

momo219
Light Poster
44 posts since Dec 2009
Reputation Points: 11
Solved Threads: 3
 

Thanks a mil!

strongpot
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: