ANKARA (Reuters) - A Turkish court on Monday ordered the detention of a former opposition lawmaker hours after a lower court freed him on bail while he is being tried on terrorism-related charges, state-owned Anadolu news agency said.

Eren Erdem, who lost his seat in the mid-2018 election that granted President Tayyip Erdogan sweeping new powers, has been jailed since June and accused of publishing illegal wiretaps while editor of an opposition newspaper in 2014.

He denies charges of assisting followers of U.S.-based cleric Fethullah Gulen, who is accused of orchestrating a failed 2016 putsch.

Prosecutors sought a sentence of up to 19 years for Erdem over “membership of an armed organization” and aiding terrorists, Anadolu said. However, the court ruled on Monday that he should be released on bail.

Hours later, a higher court overturned the release ruling, following an appeal by the Istanbul prosecutor’s office on grounds that Erdem may flee.

He is the second politician from the secularist Republican People’s Party (CHP) to be detained in a sweeping security crackdown since the attempted military coup that has sparked outrage from rights groups and some of Turkey’s Western allies.

The other CHP politician, Enis Berberoglu, was initially sentenced to 25 years in jail on espionage charges in June 2017, but was later released in September. He also denies the charges against him.

The cleric Gulen, a former ally of President Tayyip Erdogan who now lives in the United States, denies any connection with the 2016 attempt to overthrow the government.

Since then, 77,000 people have been formally arrested, while more than 150,000 civil servants, military personnel and others have been sacked or suspended from jobs over alleged links to Gulen’s network.

Foes have criticized the scale of the crackdown, saying Erdogan was using the putsch as an excuse to quash dissent. The government has said the measures are justified by the gravity of the threat.

There was no immediate reaction from Erdem’s family or supporters.