Php 返回空值的查询

Php 返回空值的查询,php,mysql,ajax,Php,Mysql,Ajax,我有三张关于药物和库存的表格。第一个表是一个简单的id和med\u name,它被称为medicing 第二张表是关于每种药物以及我们有多少药片。名为med_pharmacy的表 第三个表是我们从每一位医生身上给的钱。它被称为consultation\u-med 现在,我创建了一个AJAX请求,以便在添加到数据库之前,当我们想给患者一些药片时,看看我们是否还有。如果我们仍然有,我将回显良好,如果没有,我回显超过 以下是表格: 我的问题是,在初始化时,我的意思是,当我们为每例药物获得数量为med

我有三张关于药物和库存的表格。第一个表是一个简单的
id
med\u name
,它被称为
medicing

第二张表是关于每种药物以及我们有多少药片。名为
med_pharmacy
的表

第三个表是我们从每一位医生身上给的钱。它被称为
consultation\u-med

现在,我创建了一个AJAX请求,以便在添加到数据库之前,当我们想给患者一些药片时,看看我们是否还有。如果我们仍然有,我将回显
良好
,如果没有,我回显
超过

以下是表格:

我的问题是,在初始化时,我的意思是,当我们为每例药物获得数量为
med_id=16
的药片时,我会在表2中增加一行,但不会在表3中,因为我们仍然没有向任何患者提供任何药片

因此,当我第一次为每种药物使用以下脚本时,按钮
id=add\u more
将保持禁用状态,因为查询返回null,并且表3中没有至少一条该药物的记录。因此,如果我第一次需要给一个病人发20粒药丸,我将无法点击按钮,即使我已经从医学院得到了100粒药丸

我怎样才能解决这个问题?我是否应该在表3的
给定数量
字段中添加一个填充有
0
的空行,每次都有一包新的药物出现,以便解决此问题

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

我已经找到了如下解决方案:

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

我认为应该在db查询中使用左连接。此外,您可能需要使用更多的状态,而不仅仅是“超过”和“良好”。我建议将JSON对象从php发送回jQuery。在这样的对象中,您可以存储更多的信息,这将允许您区分缺货和从未管理过的信息。不要在表格中插入不必要的空行。您可以帮助jquery部分。我不知道如何获取单个行并将其发送回jqueryor,或者如何使用左连接,以便在不存在表的情况下,可以显示空值