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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错