second查询在硬编码时有效,但在用post变量替换where条件时无效

ogsagwnx  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(241)

我试图在一个数据表中显示以下代码的输出。现在,只显示第一个查询的输出。当我在第二个查询中将$opr\u emp\u id替换为硬编码的值(比如说4563)时,这个方法可以很好地工作—我得到两个结果集。下面是fetch.php示例:

<?php
//fetch.php

include_once("DB_Config.php");

$data = array();
$opr_emp_id = trim(mysqli_real_escape_string($conn,$_POST["opr_id"]));

$query_PE = "SELECT power_equipment_training.PE_TRAINING_ID AS TRAINING_ID, 
                power_equipment_training.PE_training_name AS TRAINING_NAME, 
                power_equipment_docs.PE_DOC_ID AS DOCUMENT_ID, 
                power_equipment_docs.PE_doc_name AS DOCUMENT_NAME, 
                power_equipment_docs.PE_doc_latest_version AS LATEST_VERSION, 
                emp_power_equipment.PE_doc_version_trained AS VERSION_TRAINED, 
                emp_power_equipment.PE_training_received_date AS TRAINED_DATE, 
                emp_power_equipment.PE_training_validity AS VALIDITY,
                emp_power_equipment.PE_qualification_status AS QUALIFICATION_STATUS 
            FROM emp_power_equipment 
            INNER JOIN ( power_equipment_training INNER JOIN 
                        power_equipment_docs ON 
                        power_equipment_training.PE_TRAINING_ID = power_equipment_docs.PE_TRAINING_ID 
                        )  
                ON emp_power_equipment.PE_DOC_ID = power_equipment_docs.PE_DOC_ID 
                WHERE emp_power_equipment.emp_id='".$opr_emp_id."'";

$result_PE = mysqli_query($conn, $query_PE);
while($row_PE = mysqli_fetch_array($result_PE)) {
     $sub_array = array();
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="DOCUMENT_ID">' . $row_PE["DOCUMENT_ID"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="DOCUMENT_NAME">' . $row_PE["DOCUMENT_NAME"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="TRAINING_ID">' . $row_PE["TRAINING_ID"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="TRAINING_NAME">' . $row_PE["TRAINING_NAME"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="VERSION_TRAINED">' . $row_PE["VERSION_TRAINED"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="LATEST_VERSION">' . $row_PE["LATEST_VERSION"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="TRAINED_DATE">' . $row_PE["TRAINED_DATE"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="VALIDITY">' . $row_PE["VALIDITY"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_PE["DOCUMENT_ID"].'" data-column="QUALIFICATION_STATUS">' . $row_PE["QUALIFICATION_STATUS"] . '</div>';
     $sub_array[] = '<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row_PE["DOCUMENT_ID"].'">Delete</button>';
     $data[] = $sub_array;
}
$query_EMS = "SELECT EMS_procedure_training.EMS_TRAINING_ID AS TRAINING_ID, 
                EMS_procedure_training.EMS_training_name AS TRAINING_NAME, 
                EMS_procedure_docs.EMS_DOC_ID AS DOCUMENT_ID, 
                EMS_procedure_docs.EMS_doc_name AS DOCUMENT_NAME, 
                EMS_procedure_docs.EMS_doc_latest_version AS LATEST_VERSION, 
                emp_EMS_procedure.EMS_doc_version_trained AS VERSION_TRAINED, 
                emp_EMS_procedure.EMS_training_received_date AS TRAINED_DATE, 
                emp_EMS_procedure.EMS_training_validity AS VALIDITY, 
                emp_EMS_procedure.EMS_qualification_status AS QUALIFICATION_STATUS 
            FROM emp_EMS_procedure 
                INNER JOIN ( EMS_procedure_training 
                            INNER JOIN EMS_procedure_docs ON 
                                EMS_procedure_training.EMS_TRAINING_ID = EMS_procedure_docs.EMS_TRAINING_ID
                            ) 
                    ON emp_EMS_procedure.EMS_DOC_ID = EMS_procedure_docs.EMS_DOC_ID 
            WHERE emp_EMS_procedure.emp_id='".$opr_emp_id."'";

$result_EMS = mysqli_query($conn, $query_EMS);
while($row_EMS = mysqli_fetch_array($result_EMS)) {
     $sub_array = array();
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="DOCUMENT_ID">' . $row_EMS["DOCUMENT_ID"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="DOCUMENT_NAME">' . $row_EMS["DOCUMENT_NAME"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="TRAINING_ID">' . $row_EMS["TRAINING_ID"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="TRAINING_NAME">' . $row_EMS["TRAINING_NAME"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="VERSION_TRAINED">' . $row_EMS["VERSION_TRAINED"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="LATEST_VERSION">' . $row_EMS["LATEST_VERSION"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="TRAINED_DATE">' . $row_EMS["TRAINED_DATE"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="VALIDITY">' . $row_EMS["VALIDITY"] . '</div>';
     $sub_array[] = '<div data-id="'.$row_EMS["DOCUMENT_ID"].'" data-column="QUALIFICATION_STATUS">' . $row_EMS["QUALIFICATION_STATUS"] . '</div>';
     $sub_array[] = '<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row_EMS["DOCUMENT_ID"].'">Delete</button>';
     $data[] = $sub_array;
}
$output = array(
    "draw"    => intval($_POST["draw"]),
    "data"    => $data
);

echo json_encode($output);

?>
ajax调用:

$(document).ready(function(){

    fetch_data();

    function fetch_data() {
        var dataTable = $('#opr_training-dt').DataTable({
                    "processing" : true,
                    "serverSide" : false,
                    "order" : [],
                    "ajax" : {
                        url:"fetch.php",
                        type:"post",
                        data:{opr_id:<?php echo $opr_emp_id; ?>}                                                    
                }
        });
    }
}

前端:

<table id="opr_training-dt" class="table table-hover table-bordered responsive" cellspacing="0" width="100%">
    <thead class="thead-dark">
        <tr>
            <th scope="col">DOCUMENT ID</th>
            <th scope="col">DOCUMENT NAME</th>
            <th scope="col">TRAINING ID</th>
            <th scope="col">TRAINING NAME</th>
            <th scope="col">VERSION TRAINED</th>
            <th scope="col">LATEST VERSION</th>
            <th scope="col">TRAINED DATE</th>
            <th scope="col">VALIDITY</th>
            <th scope="col">QUALIFICATION STATUS</th>
            <th></th>                               
        </tr>
    </thead>

</table>

我不知道为什么会这样。我是php的新手。感谢您的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题