duanhuoyao7011 2018-05-10 22:20
浏览 148
已采纳

使用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 2018-05-14 19:47
    关注

    Change

    $nom = $_POST['nom'];

    to

    $nom = $_REQUEST['nom'];

    and everything will work fine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出