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