$host = "localhost"; $usuario = "TU_USUARIO"; $password = "TU_PASSWORD"; $base_datos = "TU_BASE_DE_DATOS"; try { $conexion = new PDO( "mysql:host=$host;dbname=$base_datos;charset=utf8mb4", $usuario, $password ); $conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception("Método no permitido"); } $respuesta = isset($_POST['respuesta']) ? intval($_POST['respuesta']) : 0; if ($respuesta < 1 || $respuesta > 5) { throw new Exception("Respuesta inválida"); } $sql = "INSERT INTO encuesta_satisfaccion (respuesta) VALUES (:respuesta)"; $stmt = $conexion->prepare($sql); $stmt->execute([ ':respuesta' => $respuesta ]); echo json_encode([ 'success' => true, 'message' => 'Respuesta guardada correctamente' ]); } catch (Exception $e) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); } ?>