douzi1117 2014-12-24 09:32
浏览 194
已采纳

从2个表中获取数据并插入另一个表中

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{ 
  • 写回答

4条回答 默认 最新

  • dongwuwan5646 2014-12-24 09:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿