我需要你的帮助..
我正在尝试从两个表中检索数据并使用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)
VALUES
(now(),'$ _ POST [description]','$ dept_name','$ emp_id','$ _ POST [to]')“;
$result = mysql_query($query);
if(!$result)
{die("Query got problem").(mysql_error());}
else{
答案 0 :(得分:0)
反引号可能会在这里发挥作用。
$query = "INSERT INTO Student (`date`, `description`, `from`, `emp_id`, `to`)
VALUES
(now(),'$_POST[description]','$dept_name','$emp_id','$_POST[to]')";
答案 1 :(得分:0)
试试这个:
你的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);
如果您需要进一步的帮助,请告诉我。
答案 2 :(得分:0)
试试这个
$query = "INSERT INTO Student (date, description, from, emp_id, to)
VALUES
(now(),' " . $_POST['description'] ."','$dept_name','$emp_id','". $_POST['to']. "')";
答案 3 :(得分:0)
下面的工作代码
$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());
}