MySQL查询没有被执行

时间:2017-06-13 21:02:46

标签: php mysql mysqli

我有一张表格,我可以通过POST获取数据。我还使用两个查询使用该数据更新两个单独的表。我的第一个查询运行正常,它按预期将数据插入表中,我的第二个查询几乎相同,但是将数据插入到另一个表中并且它不起作用。我的代码是这样的。

 // First good query is here.
 $selected_employee = $_POST['employees'];
 $date = date('y-m-d H:i:s');
 $qry4 = "INSERT INTO employee_leads (emp_id_fk, lead_id_fk, subject_fk, 
 date) 
VALUES ('$selected_employee', '{$_GET['id']}','$subject' , '$date' )";
$result4 = mysqli_query($con, $qry4);

// second query which isn't working
$qryChosen = " INSERT INTO lead_status (lead_id_fk, buss_id_fk, emp_id_fk) 
VALUES ('{$_GET['id']}', '$userid', '$selected_employee') ";
$resultChosen= mysqli_query($con,$qryChosen);
if(!$resultChosen) {echo "Employee record error #400 "; }

似乎它也没有迭代if(!$resultChosen)语句,因为我没有得到关于查询的回声没有针对数据库执行。谢谢。

编辑我已添加此代码$rowChosen = mysqli_fetch_assoc($resultChosen);,因为如果查询出现问题,它会显示它,并且它给了我错误 mysqli_fetch_assoc()期望参数1为mysqli_result,布尔值为  据我所知,当查询错误时我会发生这种情况并且我不确定那里可能出现什么问题? P.S $userid在页面顶部定义,应该可以访问。

EDIT-2 添加了echo "The wrong is at ". mysqli_error($con);,它绝对没有显示任何错误。

2 个答案:

答案 0 :(得分:0)

请不要使用已弃用的mysql_query。请改用PDO。

<?php
 $con = new PDO("mysql:host=$host;dbname=$db;", $user, $pw);

// First good query is here.
 $selected_employee = $_POST['employees'];
 $date = date('y-m-d H:i:s');
 $qry4 = "INSERT INTO employee_leads (emp_id_fk, lead_id_fk, subject_fk, 
 date) VALUES (:selected_employee, :get_id, :subject , :date )";
 $result4 = $con->execute(array(
 ":selected_employee" => $selected_employee,
 ":get_id" => $_GET['id'],
 ":subject" => $subject,
 ":date" => $date
 ));
 if($result4) {
 // second query which isn't working
 $qryChosen = "INSERT INTO lead_status (lead_id_fk, buss_id_fk, emp_id_fk) 
 VALUES (:get_id, :user_id, :selected_employee)";
 $resultChosen = mysqli_query($con, $qryChosen);
 $resultChosen = $con->execute(array(
 ":get_id" => $_GET['id'],
 ":selected_employee" => $selected_employee,
 ":user_id" => $userid
 ));
    if(!$resultChosen) {
    echo "Employee record error #400 ";
    }
 }
 $rowChosen = $resultChosen->fetch(PDO::FETCH_ASSOC);
?>

除此之外,我真的不明白你的问题是什么,请进一步解释。

答案 1 :(得分:-1)

首先检查表格和列中的拼写错误,然后尝试删除距离第11行和12

的空格
<?php
// First good query is here.
 $selected_employee = $_POST['employees'];
 $date = date('y-m-d H:i:s');
 $qry4 = "INSERT INTO employee_leads (emp_id_fk, lead_id_fk, subject_fk, 
 date) 
VALUES ('$selected_employee', '{$_GET['id']}','$subject' , '$date' )";
$result4 = mysqli_query($con, $qry4);
if($result4) {
// second query which isn't working
$qryChosen = "INSERT INTO lead_status (lead_id_fk, buss_id_fk, emp_id_fk) 
VALUES ('{$_GET['id']}', '$userid', '$selected_employee')";
$resultChosen = mysqli_query($con, $qryChosen);
    if(!$resultChosen) {
    echo "Employee record error #400 ";
    }
}
?>