1
でMySQLにデータを送信し、私はこのコードで自分のデータを取得することができます。Angular2:私はAngular2とMySQLとPHPでのCRUDシステムを設定しようとしていますPHPファイル
getClients(){
this.http.get('http://localhost/Angular%20tests/pr1/getClients.php')
.subscribe(res=>this.clients=res.json(),
error=> alert("Error get clients"),
()=> console.log("Get clients completed..."));
}
だけにデータを送信しますサーバー、私は理解できない私のエラーは、最初の3つの指示は、私のエントリの正しい値を与えています。
onPersonneF(f:NgForm){
console.log(f.value);
console.log(JSON.stringify(f.value));
console.log(f.valid);
// test for the post
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
this.http.post('http://localhost/Angular%20tests/pr1/insertClient.php', JSON.stringify(f.value), options);
}
そして、私のPHPファイルのコードは次のとおりです。
<?php
// for: Blocage d'une requête multi-origines (Cross-Origin Request)
header("Access-Control-Allow-Origin: *");
try {
$pdo = new PDO("mysql:host=localhost; dbname=test; char=utf8", '****', '****');
} catch (Exception $e) {
echo "Connexion Error".$e->getMessage();
}
$data = json_decode(file_get_contents("php://input"));
//var_dump($data);
echo "**************";
echo $data->nom_client;
echo "**************";
$nom_client = $data->nom_client;
$prenom_client = $data->prenom_client;
$no_order = $data->no_order;
$query = $pdo->prepare("insert into clients values(NULL,'".$nom_client."', '".$prenom_client."', '".$no_order."')");
$query->execute();
$query->closeCursor();
?>
** WARNING ** :)戻ってきているものを
json_encode
に覚えておいてください.php)をプレースホルダ値で置き換え、 'execute'の引数としてユーザデータだけを与えます。このコードでは、潜在的に深刻な[SQLインジェクションのバグ](http://bobby-tables.com/)があります。このような問題を避ける方法については、[PHP The Right Way](http://www.phptherightway.com/)を参照してください。 – tadman