PHP POST FORM VALIDATION

I’m really NEW at php and trying to learn the best I can. I’ve figured out on my own(and the net of course) to build a form using POST to save data to a SQL database. I cannot for the life of me find anything to help me validate the data. I’m sorry if this is sloppy code or a really STUPID QUESTION to the experts online answering forum posts on other sites (which is the vibe I get from them).

I’m hoping it’s easier to learn here. Any help or a point in the right direction would be awesome. I’m just stuck is all. Thanks.

Form:

Student Application Form

Which campus are you applying to?

campus1 campus2 campus3 campus4

Birth Date:

Contact Information:

Firstname:

Lastname:

E-mail:

Phone:

Cell:

Address:



etc. etc. Scripting:

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ’ . mysql_error());

}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected) {
die('Can’t use ’ . DB_NAME . ‘:’ . mysql_error());
}

$campus = $_POST[‘campus’];
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
$transportation = $_POST[‘transportation’];/checkbox variable/
$transporttype = $_POST[‘transporttype’];
$messagebox = $_POST[‘messagebox’];
$email = $_POST[‘email’];
$dob = $_POST[‘dob’];
$uscitizen = $_POST[‘uscitizen’];
$veteran = $_POST[‘veteran’];
$transfer = $_POST[‘transfer’];
$working = $_POST[‘working’];
$ftpt = $_POST[‘ftpt’];
$employer = $_POST[‘employer’];

$sql = “INSERT INTO application (firstname, lastname, transportation, transporttype, messagebox,
campus, email, dob, uscitizen, veteran, transfer, working, ftpt, employer) VALUES
(’$firstname’,’$lastname’,’$transportation’,’$transporttype’,’$messagebox’,’$campus’,
‘$email’,’$dob’,’$uscitizen’,’$veteran’,’$transfer’,’$working’,’$ftpt’,’$employer’)”;

if (!mysql_query($sql)) {
die('Error: ’ . mysql_error());
}

mysql_close();

?>

This is coming from a personal perspective, but before you worry about validation of your form concentrate on the basics.

First thing to realize is the Internet has a bunch of misleading tutorials or tutorials that are outdated, which to a person learning the PHP language can lead you down the wrong rabbit hole. What I mean the first step on the php side is to learn/use mysqli or PDO (my recommendation) instead of mysql for that is obsolete, visit www.php.net then do a search on mysqli or PDO. You can get a lot of good info just from that website, but people here will help you as well. I know it can be frustrating for a person learning php to run into a person who tries to help you out, for they sometime come across as rude (I know I might have from time to time). However, it’s just as equally as frustrating to the person showing the person how to implement mysqli or PDO to have that person write “I just want to learn PHP, I learn PDO or mysqli latter on when I get the time”. Well, they never seem to get time and never learn the newer way of doing things, but there will be a time in the future where they will be force to. Then you have experience php programmers who either thrown in the towel or have been doing it for so long that they gladly show you how to do it the old way (mysql). I personally would want to learn the new way and I admit I had experienced programmers steer me in the right direction when I was first starting out. It was because I came across an old php tutorial and was following it. Take my advice or not, but I hope you do. Well, enough of my soapbox. I’ll take a look at your script and get back to you. An if someone gives you help using mysql and concentrating on validation I’ll take no offense to it. I’ll just move along or continue to help you way that I feel is proper.

Thank you so much. I really just want to learn how to attach a form to my servers database securely so I can take data because my boss asked me to. Any help is very much appreciated. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service