使用$ http.get检索单个记录

时间:2015-12-31 17:10:07

标签: javascript php sql angularjs

我在PHP文件中有一些函数可以从服务器数据库写入和请求数据。这些php函数的请求来自js文件。我写入服务器的第一个功能是可行的,但是我在控制台中没有错误的路障,继续检索单个记录。

目前,我相信我已正确提取数据,因为我可以在控制台日志中看到它,但数据似乎没有分配给变量。

我的PHP文件

    switch($_GET['action']){
case 'writeInvoice':
        writeInvoice();
        break;
case 'fetchInvoice':
        fetchInvoice($_GET['num']);
        break;
}

function fetchInvoice($num){

$fqry = "SELECT customerName,
                dealerName,
                billTo,
                billStart,
                contractTerms,
                item,
                itemPrice,
                quantity,
                sharePercent,
                cost
                FROM invoice
                WHERE idInvoice = '{$num}'";


$qry = new Query($fqry,__LINE__,__FILE__);
$result = json_encode($qry->fetch_array());
print_r($result);


return $result;
}

我的js /角度代码

    $scope.editInvoice  = function($param){

    $http.get('DealerRec/writeInvoice.php?num='+$param+'&action=fetchInvoice', $scope.list)
        .success(function(data,status,headers,config){console.log("Data Taken"); console.log(data);
         fetchData = data; 
          $scope.list.idInvoice     = $param;
    $scope.list.customerName  = fetchData.customerName;
    $scope.list.dealerName    = fetchData.dealerName;
    $scope.list.billTo        = fetchData.billTo;
    $scope.list.billStart     = fetchData.billStart;
    $scope.list.item          = fetchData.item;
    $scope.list.price         = fetchData.price;
    $scope.list.qty           = fetchData.qty;
    $scope.list.contractTerms = fetchData.contractTerms;
    $scope.list.per           = fetchData.per;
    $scope.list.cost          = fetchData.cost;

 })
        .error(function(data,status,headers,config){ console.log("Data Not Taken"); });



    CustomModal.setOption(2);
};

我的目标是通过发票ID#从数据库中检索发票,并存储我可以查看并在其上书写的字段。

当我调用函数时,我的控制台显示了这一点,例如,条目12

Data Taken

(
    [num] => 12
    [action] => fetchInvoice
 )
                                          {"customerName":"SomeCustomer","dealerName":"SomeDealer","billTo":"Customer","billStart":"2015-06-10","contractTerms":"Billing Software    Terms","item":"SomeItem","itemPrice":"150.00","quantity":"20","sharePercent":"75","cost":"15.00"}

当函数执行时,它在最后一行打开一个模态,数据设置/可编辑。

1 个答案:

答案 0 :(得分:0)

以下部分肯定是错误的:

fetchData = $http.get('DealerRec/writeInvoice.php?num='+$param+'&action=fetchInvoice', $scope.list)
    .success(function(data,status,headers,config){console.log("Data Taken"); console.log(data); })
    .error(function(data,status,headers,config){console.log("Data Not Taken"); });

您无法将此$http服务的结果分配给fetchData。您需要在成功回调中执行此操作:

$http.get('DealerRec/writeInvoice.php?num='+$param+'&action=fetchInvoice',$scope.list)
    .success(function(data,status,headers,config){
        console.log("Data Taken"); 
        console.log(data); 
        fetchData = data; // this is what you have to do
    })
    .error(function(data,status,headers,config){ 
        console.log("Data Not Taken"); 
    });