amigos.
Estoy empezando con Swift y pretendo rellenar mediante el método POST
mi BBDD y lamentablemente no sé dónde estoy cometiendo el error, o en el php o en el swift. BBDD Mysql.
Codigo PHP
if(isset($_POST["Name"])){
$Name= ( empty($_POST['Name']) ) ? NULL : $_POST['Name'];
$Room= ( empty($_POST['Room']) ) ? "" : $_POST['Room'];
$Teacher_Name= ( empty($_POST['Teacher_Name']) ) ? "" : $_POST['Teacher_Name'];
if ($name){
if( $conexion=mysqli_connect($hostname,$username,$password,$database) ){
$sql= "INSERT INTO classes(Name,Room,Teacher_Name) VALUES (?,?,?)";
if ($stmt = mysqli_prepare($conexion, $sql)) {
mysqli_stmt_bind_param($stmt,"sss",$Name,$Room,$Teacher_Name);
if( mysqli_stmt_execute($stmt) ) {
//$msg=printf("Insert", mysqli_stmt_affected_rows($stmt));
$msg=printf("");
}else{
//$msg=printf("Error: %s ejecutando la consulta", mysqli_stmt_error($stmt));
$msg=printf("Account already exists");
}
mysqli_stmt_close($stmt);
}else{
$msg=printf("Error: %s preparando la consulta", mysqli_error($conexion));
}
mysqli_close($conexion);
}else{
$msg=printf("Error de conexión: %s", mysqli_connect_error());
}
}else{
$msg="El nombre estaba vacío";
}
//Envia un numero de error al final
//echo $msg;
}
Codigo Swift
@IBAction func onPostTapped(_ sender: Any) {
let parameters = ["Name": "Orlando", "Room": "RDR", "Teacher_Name": "Tea Lopez"]
guard let url = URL(string: "http://zrerdo.com/Arcmmetry/postClasses.php") else {return}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody
let session = URLSession.shared
//usar Data URLRequest
session.dataTask(with: request ) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
//Elegimos jsonObject InputStream
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
}
catch{
print(error)
}
}
}.resume()
}
Muchas gracias por adelantado.