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 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥20 白日门传奇少一个启动区服和启动服务器的快捷键,东西都是全的 , 他们说套一个出来就行了 但我就是弄不好,谁看看,
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题