Based upon your code, as written, your session will not start and $_SESSION will be unusable for your.
The session_start(); must be on the
absolute first line.
<?php session_start();
That alone may help you well along your way - but if you don't tell us exactly what the program error is then we're not going to do better than take a guess.
Does your SQL query fail? Does it not even run? If it doesn't run, is it a connection error or something else?