Php 使用formData将数据发送到mysql
我在从服务接收数据到API时遇到问题Php 使用formData将数据发送到mysql,php,mysql,angular,api,httprequest,Php,Mysql,Angular,Api,Httprequest,我在从服务接收数据到API时遇到问题 insertService(nom: string, fileToUpload: File) { console.log(nom); const url = 'http://localhost/Admin/src/api/api.php?action=insertService&nom=' + nom ; const formData: FormData = new FormData(); formData.appen
insertService(nom: string, fileToUpload: File) {
console.log(nom);
const url = 'http://localhost/Admin/src/api/api.php?action=insertService&nom=' + nom ;
const formData: FormData = new FormData();
formData.append('nom', nom);
formData.append('image', fileToUpload, fileToUpload.name);
return this.http.post(url, formData);
}
这就是API
$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);
$nom = $_POST['nom'];
$image = $_POST['image'];
$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,date_s,image_ser) values("' . $nom . '","' . Null . '","' . $filepath . '")';
$result = $db->query($query);
if ($result === TRUE) {
$res = "Inserted'$nom '";
echo json_encode($res);
} else {
echo json_encode("Error" . $query . "<br>" . $db->error);
}
break;
$rest\u json=file\u get\u contents('php://input");
$\u POST=json\u decode($rest\u json,true);
$nom=$_POST['nom'];
$image=$_POST['image'];
$filetmp=$\u文件[“图像”][“tmp\u名称”];
$filename=$_文件[“图像”][“名称”];
$filepath=“../assets/img/”$_文件[“图像”][“名称”];
移动上传的文件($filetmp,$filepath);
$query='插入etservice(名称服务、日期、图像服务)值(“.$nom.”、“.Null.”、“.$filepath.”);
$result=$db->query($query);
如果($result==TRUE){
$res=“插入“$nom”;
echo json_编码($res);
}否则{
echo json_encode(“错误”。$query。“
”$db->Error);
}
打破
问题是我无法在API中接收“nom”值,并且它不会显示任何错误。
有人能帮我吗
I think you are missing the headers part of the provided code:
insertService(nom: string, fileToUpload: File) {
console.log(nom);
const url = 'http://localhost/Admin/src/api/api.php?action=insertService&nom=' + nom ;
const formData: FormData = new FormData();
formData.append('nom', nom);
formData.append('image', fileToUpload, fileToUpload.name);
let headers = new Headers();
headers.append('enctype', 'multipart/form-data');
headers.append('Accept', 'application/json');
// headers.append('Authorization', 'Bearer ' + Authtoken);
// uncomment if needed
let options = new RequestOptions({headers: headers});
return this.http.post(url, formData, , options);
}
希望上面的代码可以工作它告诉我找不到请求选项它也不工作,它没有显示错误,即使我更改了url,也没有插入值nom,但仍然不工作