当我将键的值更新为0

时间:2016-10-16 13:10:52

标签: php ajax

我有一个代码,用于在销售模式(在发票页面上)进行销售,并带有数据库条目。当选择销售产品时,可将该产品的可用件输入另一个输入并在提交时,产品产品的ID和件已添加到会话数组中。这是因为在销售时将其添加到发票页面上的销售表中,以防用户决定添加或减去根据销售表上已有的特定产品的数量,让销售模式(可用的部分)输入在要显示的值上是最新的,而不是再次从数据库中获取。

这是我用于制作销售数据库条目和设置会话数组值的代码示例:

    $productPieces = get_productPiecesForSale();//fetches product pieces from db
    $product_id = $_POST['product_id'];//product id
    $pieces = $_POST['pieces_sold'];// pieces sold
    $invoice_id = $_SESSION['invoice_id'];

    $query = "SELECT * FROM sales WHERE product_id = '$id' AND invoice_id = '$invoice_id' LIMIT 1";// check if product is in the sales table already
    $sales_set = mysqli_query($connect,$query);
    $sales = mysqli_fetch_array($sales_set);

    if(isset($sales['sales_key'])){

    $pieces_update = $pieces + $sales['pieces']; // adds db pieces to pieces sold

    if($pieces_update > $productPieces){

    if(in_array($product_id, $_SESSION['pieces_available'])){
    $key = array_search($product_id , $_SESSION['pieces_available']);
    $_SESSION['pieces_available'][$key+1] = '0';
    }  

//so that pieces can't be sold above what is available
    $query = "UPDATE sales SET pieces = '{$productPieces}'  WHERE product_id = '{$product_id }' LIMIT 1";
    $sales_set = mysqli_query($connect,$query);
    }else{

    if(in_array($product_id , $_SESSION['pieces_available'])){
    $key = array_search($product_id , $_SESSION['pieces_available']);
    $_SESSION['pieces_available'][$key+1] = $productPieces - $pieces_update;
    }

    $query = "UPDATE sales SET pieces = '{$pieces_update}'' WHERE  product_id = '{$product_id }' LIMIT 1";
    $sales_set = mysqli_query($connect,$query);
    }//if($pieces_update > $productPieces){

    }else{

    $_SESSION['pieces_available'][] = $_POST['product_id'];
    $_SESSION['pieces_available'][] = $_POST['pieces_remaining'];
    $query = "INSERT INTO sales() VALUE()";
    $sales_set = mysqli_query($connect,$query);
    }

这里是我的ajax的php代码我用来填充我的销售表单输入

if(isset($_GET['p'])){
     $p = $_GET['p'];

     if(isset($_SESSION['pieces_available'])){//$_SESSION['pieces_available'] is set

       if(in_array($p, $_SESSION['pieces_available']) !== false){//product_id is in array
         $key = array_search($p, $_SESSION['pieces_available']);
         $pieces = $_SESSION['pieces_available'][$key+1];
         echo $pieces;
       }else{//product_id is not in array
    $query = "SELECT pieces FROM products WHERE product_id = '{$p}' LIMIT 1";
    $result_set = mysqli_query($connect, $query);
    $result = mysqli_fetch_array($result_set);
    echo $result['pieces'];
       }  

     }else{//$_SESSION['pieces_available'] isn't set
    $query = "SELECT pieces FROM products WHERE product_id = '{$p}' LIMIT 1";
    $result_set = mysqli_query($connect, $query);
    $result = mysqli_fetch_array($result_set);
    echo $result['pieces'];
     }

    }

一切都按照我希望它的工作方式运行,直到我更新已经存在于销售表上的产品的部分,现在剩余的部分变为0,然后我的销售表单停止显示可用于所选产品的部分。如果剩余的碎片变成任何其他数字,则不会发生这种情况。

以防万一,让我添加我的ajax和HTML代码:

<html>
<head>
<script>
 function loadDoc2(str){
     if (str == "") { 
        document.getElementById("pieces_available").value = "";
        return;
    } else {
         //Check for browsers
         if(window.XMLHttpRequest){
             xhttp = new XMLHttpRequest();
         }else{
             xhttp =  new ActiveXObject("Microsoft.XMLHTTP");
         }
         //ajax onready
         xhttp.onreadystatechange = function(){
        if(xhttp.readyState == 4 && xhttp.status == 200){
        document.getElementById("pieces_available").value = xhttp.responseText;
             }
         };
         xhttp.open("POST","url?p="+str,true);
         xhttp.send();
 }
 }
</script>
</head>

<body>
<select id='product_id' onchange='loadDoc2(this.value)'>
<option value=''>Choose products</option>
</select>
<input type='number' id='pieces_avaible' readonly/>
<input type='number' id='pieces' placeholder='enter pieces for sale'/>
</body>
</html>

销售表截屏:saleTable 销售模式屏幕截图:sale Modal

0 个答案:

没有答案