duanhuoyao7011
duanhuoyao7011
2018-05-10 22:20

使用formData从angular传递数据到php(mysql)

已采纳

I am using formData to append image information and extract it into my API using php.

I append my data like this:

    insertService(nom: string, fileToUpload: File): Promise<any> {
        const url = 'http://localhost/Admin/src/api/api.php?action=insertService';
        const formData: FormData = new FormData();

        formData.append('image', fileToUpload);

        const param = {nom: nom, image: formData};
        console.log(param);
        const request = this.http.post(url, param);
        return request.toPromise();
}

So as you can see I append my data to 'image'.

And here I am trying to extract it

 $nom = $_POST['nom'];
        $image = $_POST['image'];

        echo ('nom ' .$nom);
        echo ('image :' .$image);
        echo($_FILES["image"]["name"]);

        $filetmp = $_FILES["image"]["tmp_name"];
        $filename = $_FILES["image"]["name"];
        $filepath = "../assets/img/" . $_FILES["image"]["name"];

        move_uploaded_file($filetmp, $filepath);

        $query = 'INSERT into etservice(nom_service,image_ser) values("' . $nom . '","' . $filepath . '")';
        $result = $db->query($query);
        if ($result === TRUE) {
            $res = "Inserted'$nom' and image '$image' ";
            echo json_encode($res);
        } else {
            echo json_encode("Error" . $query . "<br>" . $db->error);
        }

It shows me this error

Notice: image : Array to string conversion

Can anybody help me please?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dpbz14739 dpbz14739 3年前

    Change

    $nom = $_POST['nom'];

    to

    $nom = $_REQUEST['nom'];

    and everything will work fine

    点赞 评论 复制链接分享

相关推荐