简体   繁体   中英

MySQL query isn't being performed

I have a form which I'm getting data from by POST. I am also updating two separate tables using that data, using two queries. my first query runs fine and it inserts data into a table as expected, my second query is almost the same, but inserts data into another table and it doesn't work. My code is like this.

 // 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 "; }

Seems like it doesn't iterate the if(!$resultChosen) statement also, since I'm not getting the echo about the query not getting performed against the database. Thanks.

EDIT I've added this code $rowChosen = mysqli_fetch_assoc($resultChosen); because if there's something wrong with the query it'll show it, and it gave me the error mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given As far as I know this happens when the query is wrong and I'm not sure what might be wrong there? PS $userid is defined at the top of the page and it should be accessible.

EDIT-2 added echo "The wrong is at ". mysqli_error($con); echo "The wrong is at ". mysqli_error($con); and it shows absolutely nothing as an error.

Don't use the deprecated mysql_query please. Use PDO instead.

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

Other than that I dont really understand what your question is, please explain it further.

First check for the typo mistakes in tables and columns, then Try to remove blank spaced from your from line 11 and 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 ";
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM