php post undefined index
I am trying to do a registration form that registers a user in a database (MySQL).
The code is supposed to register:
The code, before saving to the table, checks whether the username or password already exists, and if they do it echoes an error message, and if not, it saves to the db.
However, on page load, I’m getting these errors:
Any help would be appreciated.
4 Answers 4
You’re getting errors because you’re attempting to read post variables that haven’t been set, they only get set on form submission. Wrap your php code at the bottom in an
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’)
Also, your code is ripe for SQL injection. At the very least use mysql_real_escape_string on the post vars before using them in SQL queries. mysql_real_escape_string is not good enough for a production site, but should score you extra points in class.
From line 215, update:
POST variables are coming from your form, and you have to check whether they exist or not, else PHP will give you a NOTICE error. You can disable these notices by placing error_reporting(0); at the top of your document. It’s best to keep these visible for development purposes.
You should only be interacting with the database (inserting, checking) under the condition that the form has been submitted. If you do not, PHP will run all of these operations without any input from the user. Its best to use an IF statement, like so: