查询返回空值

时间:2017-07-19 06:01:34

标签: php mysql ajax

我有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";
}

1 个答案:

答案 0 :(得分: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);
})