Q:

Get data from 2 tables and insert into another

I need your help..
I am I trying to retrieve data from two tables and insert into another using php +mysql, but it doesn't work. It shows me this message (Query got problem).
This is my code:
$emp_id = $_SESSION['emp_id'];

$from= "select department.name from department,employee where emp_id='$emp_id' and department.dept_id = employee.dept_id ";
$result_form = mysql_query($from);

$dept_from = mysql_fetch_assoc($result_form);
$dept_name = $dept_from['department.name'];

$query = "INSERT INTO Student (date, description, from, emp_id, to)
VALUES
(now(),'$_POST[description]','$dept_name','$emp_id','$_POST[to]')"; 
$result = mysql_query($query);                                          
if(!$result)
    {die("Query got problem").(mysql_error());}
else{ 

A:

Try this:
Your mysql query in not proper use like given below:
    $emp_id = $_SESSION['emp_id'];    

    $from= "SELECT d.name FROM department d LEFT JOIN employee e ON d.dept_id = e.dept_id WHERE emp_id = '$emp_id' ";

    $result_form = mysql_query($from);
    $dept_from = mysql_fetch_assoc($result_form);
    $dept_name = $dept_from['name'];

    $query = "INSERT INTO Student (`date`, `description`, `from`, `emp_id`, `to`) VALUES (now(),'".$_POST[description]."','".$dept_name."','".$emp_id."','".$_POST[to]."')"; 

   $result = mysql_query($query); 

Let me know if you need further help.

