我想在MySQL中保存數據。我使用PHP和我的代碼:如何通過Alamofire將數據插入Swift 3中的MySQL?
<?php
header('Content-Type: text/html; charset=UTF-8');
//Connect to Database
include "Connect.php";
//getting values
$name = $_POST['name'];
$email = $_POST['email'];
//query
$q="INSERT INTO users (name, email) VALUES ($name,$email)";
if (mysqli_query($connection, $q)) {
$response['error']=false;
$response['message']="New record created successfully";
} else {
$response['error']=true;
$response['message']="Error: " . $q . "<br>" . mysqli_error($connection);
}
echo json_encode($response);
mysqli_close($connection);
,我使用Alamofire和我的代碼:
let parameters: Parameters = [
"name": "xxxx",
"email": "yyyy"
]
Alamofire.request(api, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseJSON { response in
if let data = response.data {
let json = String(data: data, encoding: String.Encoding.utf8)
print("Response: \(json)")
}
}
當我運行Xcode中,我得到這個錯誤:
Response: Optional("{\"error\":true,\"message\":\"Error: INSERT INTO users (name, email) VALUES (xxxx,yyyy)<br>\"}")
我的代碼有什麼問題?
對於初學者來說,它是對** SQL Injection **攻擊廣泛開放的。使用**參數化語句**來保護你自己,同時自動解決你的問題,因爲使用參數化語句,你不需要*將字符串值換成引號*。 –
@FranzGleichmann我不明白。 – Mayday