【问题标题】:Get data from 2 tables and insert into another从 2 个表中获取数据并插入到另一个表中
【发布时间】:2014-12-24 09:32:54
【问题描述】:

我需要你的帮助..

我试图从两个表中检索数据并使用 php +mysql 插入另一个表,但它不起作用。它向我显示了这条消息(查询有问题)。

这是我的代码:

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

价值观

(now(),'$_POST[description]','$dept_name','$emp_id','$_POST[to]')";

$result = mysql_query($query);                                          
if(!$result)
    {die("Query got problem").(mysql_error());}
else{ 

【问题讨论】:

  • 我在插入查询中看到 2 个保留字为它们使用了反引号 to from

标签: php mysql


【解决方案1】:

试试这个:

您的 mysql 查询未正确使用,如下所示:

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

如果您需要进一步的帮助,请告诉我。

【讨论】:

  • 感谢您的帮助,但没有任何改变
  • 回声$查询;变量,在 mysql 中运行查询并检查问题是什么?
  • 它显示这条消息: INSERT INTO Student (date, description, from, emp_id, to) VALUES (now(),'this is my Reason...','','',' HR')查询有问题
  • 这意味着您没有从之前的查询中获取数据。 echo $from 并检查数据。
  • 它显示 INSERT INTO Student (date, description, from, emp_id, to) VALUES (now(),'this is my Reason...','','1','HR' )Resource id #3Query 有问题
【解决方案2】:

反引号在这里可能会起作用。

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

【讨论】:

  • 感谢您的帮助,但没有任何改变。
【解决方案3】:

试试这个

 $query = "INSERT INTO Student (date, description, from, emp_id, to)

        VALUES

    (now(),' " . $_POST['description'] ."','$dept_name','$emp_id','". $_POST['to']. "')";

【讨论】:

  • 感谢您的帮助,但没有任何改变。
【解决方案4】:

下面的工作代码

$emp_id = 1;

$from= "select department.name from test.department,test.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['name'];
$date           = date("Y-m-d H:i:s");
$description    = isset($_POST[description])?$_POST[description]:"none";
$to             = isset($_POST[to])?$_POST[to]:"none";

$query = sprintf("INSERT INTO `test`.`test`
                    (`date`,
                    `description`,
                    `from`,
                    `emp_id`,
                    `to`)
                    VALUES
                    (
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s'
                    );
                    ",
                    $date,$description,$dept_name,$emp_id,$to); 


$result = mysql_query($query);   

if(!$result)
    {
        die("Query got problem").(mysql_error());
    }

【讨论】:

  • 感谢您的帮助,但没有任何改变。
  • 它现在正在工作。非常感谢。感谢您的努力和时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多