dongyuxin5353 2015-01-16 01:33
浏览 25
已采纳

如何使用php和sql应用array_diff

Hi im unable to apply array_diff() for when user edits the stockroom detail to replace with the new result.

Here is the edit function where i call out all the variables via sql.

 function einv_editStockrm($srid,$code,$name,$desc,$remark,$cat)
    {
        //connect to database
        base_connectDatabase();   

 $User = base_getUserDetail($_SESSION['uID']);
    $Stockroom = einv_getStockrmDetail($srid);
    base_executeSQL("UPDATE einv_stockroom
    SET einv_stockrm_code='" . $code . "',
    einv_stockrm_name='" . $name . "',
    einv_stockrm_desc='" . $desc . "',
    einv_stockrm_remark='" . $remark . "',
    einv_stockrm_cat = '" . $cat . "'
    WHERE einv_stockrm_id=" . $srid . "");

    base_addTransactionLog('Manage Stock Room', 'Edit',
    "
    Stock Room Code = " . $code . " ||
    Stock Room Name = " . $name . " ||
    Stock Room Description = " . $desc . " ||
    Stock Room Remark = " . $remark . " ||
    Stock Room Category = " . $cat . "
    ");

Here is the array_diff() function. There may be unnecessary calling and/or lack of coding..not too sure. This set of codings result in no output. The server basically crashes. Any help on this?

    //oldsr is existing stockroom data
    //newsr is new stockroom data through edit function
    //resultsr is the end result 
    <?php 
    $StockroomGetAllDetails = array();
    $arr2 = einv_getStockrmDetailFromCode($stockroomCode);
    $codearr = explode(",", $arr2['einv_stockrm_code']);

    $oldsr = array($codearr);
    $newsr = array($codearr);
    $resultsr = array_diff($newsr, $oldsr);

    if(!count(array_diff($oldsr, $newsr)) && !count(array_diff($newsr, $oldsr))) {
    // Arrays are equal
    } else {
    // Array values are different
        echo $resultsr;
    }
    ?> 

This code is for redirect.

        //go to stock room page
        echo '<script type="text/javascript">' . "
";
        echo 'window.location="../einventory/view_stockrm.php?id='. $srid .'";';
        echo '</script>';

    //close the database
    base_closeDatabase();
}
  • 写回答

1条回答 默认 最新

  • douke1954 2015-01-16 06:47
    关注

    This set of codes able to display the new datas added and removed accordingly :)

    $oldCat = $Stockroom["einv_stockrm_cat"];
    
    $oldCatArr = explode(",",$oldCat);
    $newCatArr = explode(",",$cat);
    
    //Debugging
    //print_r($oldCatArr);
    //print_r($newCatArr);
    $resultCatAdd = array_diff($newCatArr, $oldCatArr);
    $resultCatRemove = array_diff($oldCatArr, $newCatArr);
    
    //Debugging
    print_r($resultCatAdd);
    print_r($resultCatRemove);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗