在按钮上单击事件后多次执行查询

时间:2017-07-19 08:06:37

标签: php jquery mysql ajax

我使用以下查询解决了in this early post on SO提到的问题:

SELECT t1.med_id, 
t1.med_pharmacy_id,
t3.med_name,
t1.med_expiry, 
t1.med_barcode, 
t1.med_tablet, 
t1.med_pill, 
t1.med_received,
sum(t2.given_quantity) as given_pills,
t1.med_tablet - ((ifnull(sum(t2.given_quantity),0)*t1.med_tablet)/t1.med_pill) as still_tablets,
(t1.med_pill-sum(t2.given_quantity)) as still_pills
FROM med_pharmacy t1
LEFT JOIN consultation_med t2 USING (med_pharmacy_id,clinic_id)
LEFT JOIN medication t3 USING (med_id,clinic_id)
WHERE t1.clinic_id=:cid and t1.med_pharmacy_id=:mid  GROUP BY t1.med_pharmacy_id, t1.med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received

它运作正常。

现在再次,如果第三个表consultation_med中仍有药物没有任何行,则在执行以下AJAX-PHP代码后,查询将运行3次并将被添加3次至数据库,如果药物已经使用并且在第三个表中有一行,那么它只会被执行一次并且没有问题:

$("#add_more").on('click', function()
{
    var doctor_id = $("#doctor_list_id").val();
    var nurse_id = $("#nurse_list_id").val();
    var diagnosis = $("#diagnosis").val();
    var medication_id = $("#medication_id").val();
    var medication_quantity = $("#medication_quantity").val();
    var consultation_result = $("#consultation_result").val();
    var medication_collector = $("#medication_collector").val();
    var medication_collector_2 = $("#medication_collector_2").val();
    if(diagnosis == "select")
    {
        $("#diagnosis").css('border-color', 'red');
        $("#diagnosis").focus();
    }
    else if(doctor_id=="select" && nurse_id=="select")
    {

        $("#doctor_list_id").css('border-color', 'red');
        $("#nurse_list_id").css('border-color', 'red');
        $("#doctor_list_id").focus();
        $("#danger_message_dr").show();
    }
    else if(medication_collector == "select")
    {
        $("#medication_collector").css('border-color', 'red');
        $("#consultation_name").focus();
    }
    else if(medication_collector == "other" && medication_collector_2 == "Other")
    {
        $("#medication_collector_2").css('border-color', 'red');
        $("#medication_collector_2").on('focus', function()
        {
            $(this).val("");
        })
        //$("#medication_collector_2").focus();
    }
    else if(medication_collector_2 == "")
    {
        $("#medication_collector_2").css('border-color', 'red');
        $("#consultation_name_2").focus();
    }
    // else if(nurse_id=="select")
    // {
    //  $("#nurse_list_id").css('border-color', 'red');
    //  $("#nurse_list_id").focus();
    // }

    else if(medication_id=="select")
    {
        $("#medication_id").css('border-color', 'red');
        $("#medication_id").focus();
    }
    else if(medication_quantity=="")
    {
        $("#medication_quantity").css('border-color', 'red');
        $("#medication_quantity").focus();
    }
    else if(consultation_result=="")
    {
        $("#consultation_result").css('border-color', 'red');
        $("#consultation_result").focus();
    }
    else
    {

        if(doctor_id=="select")
        {
            doctor_id = null;
        }
        if(nurse_id=="select")
        {
            nurse_id = null;
        }
        $("#doctor_list_id").css('border-color', '#0090ff');
        $("#nurse_list_id").css('border-color', '#0090ff');
        $("#medication_id").css('border-color', '#0090ff');
        $("#consultation_result").css('border-color', '#0090ff');
        $("#medication_collector_2").css('border-color', '#0090ff');
        $("#medication_collector").css('border-color', '#0090ff');

        var cid = $("#complication_name_3").val();
        var consultation_result = $("#consultation_result").val();
        var pid = $("#patient_id").val();
        if(doctor_id == "select")
        {
            doctor_id="";
        }
        else
        {
            var doctor_id = $("#doctor_list_id").val();
        }
        if(nurse_id == "select")
        {
            nurse_id = "";
        }
        else
        {
            var nurse_id = $("#nurse_list_id").val();
        }

        var medication_collector_2 = $("#medication_collector_2").val();
        $.ajax({
            url: '../php/addConsultation.php',
            type: 'POST',
            data: {visit_id: resp, pid: pid,
                   nid: nurse_id, did: doctor_id,
                   cid: cid, diagnosis: diagnosis,
                   medication_id: medication_id,
                   medication_quantity: medication_quantity,
                   consultation_result: consultation_result,
                   patient_id: pid, medication_collector_2: medication_collector_2},
            dataType: 'TEXT',

            success:function(response)
            {
                alert("Data Added. Please add more, or close the box!");
                $("#doctor_list_id").val("select");
                $("#nurse_list_id").val("select");
                $("#complication_name_2").val("select");
                $("#diagnosis").val("select");
                $("#medication_quantity").val("");
                $("#medication_id").val("select");
                $("#consultation_result").val("");
                $("#complication_name_3").val("");
                if($("#consultation_name").val()=="MedicationCollection")
                {
                    $("#diagnosis").val("MedicationCollection");
                }
                $("#complication_name_3").val("Regular Medication Collection Visit");
                //console.log(response);
            },
            error:function(response)
            {
                console.log(response);
            }
        })
    }
});

以下是addConsultation.php的PHP脚本:

<?php
error_reporting(E_ALL);
ini_set('display_error', 1);
require_once('../php/connection.php');

$clinic_id = $_SESSION['clinic_id'];
$visit_id = $_POST['visit_id'];
$patient_id = $_POST['patient_id'];
$nid = $_POST['nid'];
$did = $_POST['did'];
$cid = $_POST['cid'];
$diagnosis = $_POST['diagnosis'];
$medication_id = $_POST['medication_id'];
$medication_quantity = $_POST['medication_quantity'];
$consultation_result = $_POST['consultation_result'];
$medication_collector_2 = $_POST['medication_collector_2'];

//echo $lastConsultId;

$ensureQuantity = "SELECT
t1.med_pharmacy_id, t1.med_id, 
sum(t2.given_quantity) as given_pills,
t1.med_tablet - ((sum(t2.given_quantity)*t1.med_tablet)/t1.med_pill) as still_tablets,
(t1.med_pill-sum(t2.given_quantity)) as still_pills
FROM med_pharmacy t1, consultation_med t2, medication t3 WHERE (t1.med_pharmacy_id = t2.med_pharmacy_id AND t1.med_id=t3.med_id
AND t1.clinic_id=:cid AND t1.med_pharmacy_id = :mid) 

GROUP BY t1.med_pharmacy_id, t1.med_id,t3.med_name, t1.med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received";
$execEnsureQuantity = $conn->prepare($ensureQuantity);
$execEnsureQuantity->bindValue(':cid', $clinic_id);
$execEnsureQuantity->bindValue(':mid', $medication_id);
$execEnsureQuantity->execute();

$res = $execEnsureQuantity->fetch();

if($nid=="select")
{
    $nid = null;
}
if($did=="select")
{
    $did = null;
}

$addConsultation = "INSERT INTO consultation(nurse_list_id, doctor_list_id,
                complication_name, diagnosis_id, visit_id, consultation_result, clinic_id,
                patient_id)
                VALUES(:nid, :did, :cid, :diagnosis, :visit_id, :consultation_result,
                :clinic_id, :patient_id)";
$execAddConsultation = $conn->prepare($addConsultation);
$execAddConsultation->bindValue(":nid", $nid);
$execAddConsultation->bindValue(":did", $did);
$execAddConsultation->bindValue(":cid", $cid);
$execAddConsultation->bindValue(":diagnosis", $diagnosis);
$execAddConsultation->bindValue(":visit_id", $visit_id);
$execAddConsultation->bindValue(":consultation_result", $consultation_result);
$execAddConsultation->bindValue(":clinic_id", $clinic_id);
$execAddConsultation->bindValue(":patient_id", $patient_id);

$execAddConsultation->execute();

$lastConsultId = $conn->lastInsertId();

$insertQuantity = "INSERT INTO consultation_med(consultation_id, med_pharmacy_id, given_quantity, date_given, medication_collector, clinic_id)
    VALUES(:consult_id, :mp_id, :gq, :dg, :medication_collector_2, :cid)";
$execInsertQuantity = $conn->prepare($insertQuantity);
$execInsertQuantity->bindValue(':consult_id', $lastConsultId);
$execInsertQuantity->bindValue(':mp_id', $medication_id);
$execInsertQuantity->bindValue(':gq', $medication_quantity);
$execInsertQuantity->bindValue(':dg', date('Y-m-d H:i:s'));
$execInsertQuantity->bindValue(':medication_collector_2', $medication_collector_2);
$execInsertQuantity->bindValue(':cid', $clinic_id);
$execInsertQuantity->execute();

echo "q_added";

?>

所以在consultation_med表中,如果药物不存在,我可以看到3条警报消息,在控制台上我看到3次q_added

如何阻止它这样做?

我看到e.preventDefault()不是解决问题的正确方法。

0 个答案:

没有答案