2017-07-19 153 views
0

我有3張關於用藥和庫存的表格。第一個表格是簡單的idmed_name,它被稱爲medication查詢返回空值

第二張表是關於每種藥物和我們有多少藥丸。該表稱爲med_pharmacy

第三張桌是我們給每個med多少錢。它被稱爲consultation_med

現在,我創建了一個AJAX請求,所以當我們想給患者一些藥片時,看看我們是否還有或沒有添加到數據庫之前。如果我們仍然有,我會迴應good,如果不是我回應exceeded

這裏是表:

enter image description here

我的問題是,在初始化,我的意思是,當我們得到藥物的數量每例如藥片有一med_id = 16,我將有一個新增加到表2中,但沒有增加到表3,因爲我們仍然沒有給任何病人任何藥物。

因此,當我第一次爲每種藥物使用以下腳本時,按鈕id=add_more將保持禁用狀態,因爲查詢返回null,並且表3沒有至少一條此藥物的記錄。所以如果我第一次需要給病人20粒藥片,我就不能點擊按鈕,即使我已經有100粒藥片了。

我該如何解決這個問題?我應該在表3中添加一個填滿0的空行到given_quantity的領域,每當一包新葯出現時,這個問題就會得到解決?

var calculate = function() 
{ 
    var quant = $('#medication_quantity').val(); 
    var med_p_id = $("#medication_id").val(); 
    console.log(med_p_id) 
    $.ajax({ 
    url: '../php/ensureQuantity.php', 
    type: 'POST', 
    data: { quant: quant, mid: med_p_id}, 
    dataType: 'TEXT', 
    success:function(resp) 
    { 
     console.log(resp); 
     if(resp=="exceed") 
     { 
     $("#add_more").prop('disabled', true); 
     $("#danger_message").show(); 
     } 
     else 
     { 
     $("#add_more").prop('disabled', false); 
     $("#danger_message").hide(); 
     } 
    }, 
    error:function(resp) 
    { 
     console.log(resp); 
    } 
    }) 
} 

有了這個PHP代碼:

$cid = $_SESSION['clinic_id']; 
$mid = $_POST['mid']; 
$quant = $_POST['quant']; 
$still=0; 
$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', $cid); 
$execEnsureQuantity->bindValue(':mid', $mid); 
$execEnsureQuantity->execute(); 

$res = $execEnsureQuantity->fetch(); 

if($res['still_pills']==null || $res['still_pills']=="") 
{ 
    $still = 0; 
} 
else 
{ 
    $still = $res['still_pills']; 
} 
if($quant>$still) 
{ 
    echo "exceed"; 
} 
else 
{ 
    echo "good"; 
} 
+0

我想你應該在你的數據庫查詢中使用左連接(s)。此外,你可能需要使用更多的狀態,不僅「超」和「好」。我會建議將JSON對象從PHP返回給jQuery。在這樣一個對象中,你可以存儲更多的信息,這將允許你區分缺貨和從不管理。不要在表中插入不必要的空行 – user1915746

+0

你可以幫助jQuery的一部分。我無法知道如何獲取單行並將其發送回jquery – droidnation

+0

或如何使用左連接,所以如果一個表不存在,我們可以顯示空值 – droidnation

回答

0

我已經找到了解決辦法,如下:

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 

和更改的jQuery腳本:

var calculate = function() 
{ 
    var quant = $('#medication_quantity').val(); 
    var med_p_id = $("#medication_id").val(); 
    console.log(med_p_id) 
    $.ajax({ 
    url: '../php/ensureQuantity.php', 
    type: 'POST', 
    data: { quant: quant, mid: med_p_id}, 
    dataType: 'JSON', 
    success:function(result) 
    { 

     var remaining = result['still_pills']; 
     if(remaining == null) 
     { 
     remaining = result['med_pill']; 
     } 
     if(quant>parseInt(remaining)) 
     { 
     //console.log('1* Quant:'+quant+'_'+remaining); 
     $("#add_more").prop('disabled', true); 
     $("#danger_message").show(); 
     } 
     else 
     { 
     console.log('2* Quant:'+quant+'_'+remaining); 
     $("#add_more").prop('disabled', false); 
     $("#danger_message").hide(); 
     } 
     // if(resp=="exceed") 
     // { 
     // $("#add_more").prop('disabled', true); 
     // $("#danger_message").show(); 
     // } 
     // else 
     // { 
     // $("#add_more").prop('disabled', false); 
     // $("#danger_message").hide(); 
     // } 
    }, 
    error:function(result) 
    { 
     console.log(result); 
    } 
    }) 
} 

$(document).ready(function() 
{ 
    $('#medication_quantity').on('keyup', calculate); 
    $('#medication_id').on('change', calculate); 
})