The terrifying incident took place at the Queens Road branch of the fast food outlet at around 9pm on March 12 this year.
Prosecuting, Michael Tooley, told Sheffield Crown Court how the student was about to collect food at the counter when defendant, Mizrian Fanty, 34, walked up behind him.
He said: "He was aware of what he thought was someone bumping into him. He turned around and saw Mr Fanty had a blade.
"The defendant struck [the complainant] about four times with the knife. Subsequently, the complainant felt blood running down his back and saw blood coming through his upper garments."
The complainant was rushed to hospital, where he was treated for four stab wounds to his lower back and the side of his chest.
In a victim personal statement read out in court, the complainant described how the incident had impacted on his life.
He said: "I can't understand why the male would do this to me. I don't know what to think."
He added: "I'm a university student, and I have deadlines to keep. I hope my work will not be affected by this."
After being arrested and charged in connection with the incident, Fanty was remanded into custody at HMP Doncaster.
The court heard how at around 11.40am on April 2, Fanty attacked a female prison officer who he had called into her cell.
Mr Tooley said CCTV evidence showed the defendant stepping out of the cell and punching the woman several times, which led to her falling to the floor and being knocked unconscious.
The attack left the woman with swelling and bruising, the court heard.
Fandy, now of HMP Armley, Leeds, pleaded guilty to one count of possession of bladed article in a public place and grievous bodily harm with intent, in relation to the KFC incident, and assault occasioning actual bodily harm relating to the attack on the prison officer, at an earlier hearing.
Psychiatrist Dr Eric Mendelson told the court that Fandy suffered from schizophrenia, and said he believed it was 'extremely unlikely' that he would have committed the offences if it was not for his condition.
Judge Peter Kelson sentenced him to a hospital order, under sections 37 and 41 of the mental health act.
He said: "It is the pyschiatrist's assessment to make a hospital. It is one which he tells me will better protect public safety than any other punishment available to me."