Find out the post ID / page ID of where you want your custom PHP code to go.
Assuming that where your custom code will go is in a post, you should create a file in your theme's folder called single-POSTID.php.
Assuming that where your custom code will go is in a page, you should create a file in your theme's folder called page-PAGEID.php.
Copy the base code from single.php or page.php into the newly created file, and you can insert your custom code in the new files. Form actions should usually be "#" and if your form isn't working appropriately, it's most likely because of one of your HTML form fields are used by Wordpress to do something. (e.g. a textbox with the name of "comment" will not properly work under your code, since Wordpress will think you're trying to add a comment).
If you do anything that requires database access, Wordpress keeps the DB connection alive in your theme, so you don't need to connect to the database again. Unless of course, whatever you're trying to do is in a separate database other then the one Wordpress is installed in.
Let me know if that helped you or if you have more questions!