0

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 = "<select name='clientid' value='clientname' id='clientname'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row}'>{$row}</option>";}
$dropdown .= "\r\n</select>";
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;
$project=$_POST;
$type=$_POST;
$target=$_POST ;
$priority=$_POST ;
$notes=$_POST;
$start=$_POST;
$status=$_POST;
$finish=$_POST;
$time=$_POST;
$perhour=$_POST;
$invoice=$_POST;

$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 "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

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();

?>

4
Contributors
11
Replies
15
Views
6 Years
Discussion Span
Last Post by strongpot
0

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

0

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.

Edited by jmichae3: n/a

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;
$project=$_POST;
$type=$_POST;
$target=$_POST ;
$priority=$_POST ;
$notes=$_POST;
$start=$_POST;
$status=$_POST;
$finish=$_POST;
$time=$_POST;
$perhour=$_POST;
$invoice=$_POST;

$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 "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
echo "<BR>";
}

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;}

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

?>

0

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

0

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

0

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

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.

0

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 :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.