如何从角度应用程序获取更新的Id以及如何从HTTP Post获取数据到PHP文件?

时间:2017-06-27 13:37:20

标签: javascript php angular

我正在尝试使用PHP更新并从我的角度应用程序添加数据到服务器。 我很震惊如何从角度应用程序中提供URL Id以进行更新,以及如何从Http POST获取值以插入到表中。这是我的代码:

getEmployee(id : number){
                const eurl='http://localhost/Angular/UpdateEmployee?id='
                const url= `${eurl}${id}`
                return this.http.get(this.empurl)
                .map(response => response.json().data as Data)
                .catch(this.HandleError)

            }

这是我的PHP for Update:

<?php 
$successreturn[]=array(
        "id"=>"any",
       "firstname"=>"any",
      "lastname"=>"any",
    "dateofbirth"=>"any",
    "city"=>"any",
    "gender"=>"any");
header("Access-Control-Allow-Origin: *");
$id=$_GET['id'];
$servername="localhost";
$username="root";
$password="sandeepchetikam";
$dbase="mydb";
$conn=mysqli_connect($servername,$username,$password,$dbase);
if (!$conn) {
    echo "Connection Problem".mysqli_connect_error($conn);
}

$sql= "SELECT * FROM Employees WHERE id='$id'";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
    $value=0;
if(!$result){
    echo "Connection Failed " .mysqli_connect_error($result);
 }
 while($row = mysqli_fetch_assoc($result)){

    $successreturn[$value]['id']=$row['id'];
         $successreturn[$value]['firstname']=$row['firstname'];
         $successreturn[$value]['lastname']=$row['lastname'];
         $successreturn[$value]['dateofbirth']=$row['dateofbirth'];
         $successreturn[$value]['city']=$row['city'];
         $successreturn[$value]['gender']=$row['gender'];
         $value++;

        };
echo json_encode($successreturn);


?>

addEmployees的代码:

addEmployees(data: Data): Observable<Data>{
                let headers = new Headers({'Content-Type': 'application/json'});
                let options = new RequestOptions({headers: headers});
                return this.http.post(this.empurl,data,options)
                        .map(this.extractData)
                        .catch(this.HandleError)
            }

我想知道如何编写getEmployee()函数的链接。 以及如何为addEmployees编写PHP。

0 个答案:

没有答案