PHP error codes

Hi I have the following PHP code on my web page, which when run produces error codes.

The php:

[php]
if(isset($_POST[‘Register’])) {

	session_start();
	$PID = $_POST['PID'];
	$FName = $_POST['FN'];
	$MN = $_POST['MN'];
	$LN = $_POST['LN'];
			
	$sql = $con->query("INSERT INTO person (PID, FN, MN, LN)Values('{$PID}','{$FName}','{$MN}','{LN}')");
	}

[/php]

The error codes:

Notice: Undefined index: FN in C:\xampp\htdocs\MyTree\index.php on line 9

Notice: Undefined variable: con in C:\xampp\htdocs\MyTree\index.php on line 13

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\MyTree\index.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\MyTree\index.php on line 13

I don’t see what the problem is - I am new to PHP but there must be a problem.

The database fields are the same as in the $_POST.

Any help would be appreciated.

Thanks

First prepared statements.

Second, What and where is $con.

Third, where is the form html?

To elaborate on astonecipher’s answer.

  1. The second last line of that code is that is running your “query” is expecting $con to be some kind of connection, either mysqli or pdo I assume. I won’t go into that in detail but found this neat tutorial that goes into it in some detail. http://codular.com/php-mysqli

  2. Covered in 1.

  3. Assuming all of the data that you are expecting from $_POST vars are being sent in a form. You are missing a form element with the name ‘FN’. If you don’t always need this to be set, make sure you validate the value before you assign it to a variable. This gives you a way to send back error messages or fire off some other code if it doesn’t exist.

e.g [php]
if (!isset($_POST[‘FN’])) {
echo ‘You tried to submit without the good ole FN thing.’;
exit;
}
[/php]

Lastly, it’s always a good idea to do a bit of research about the code you’re using. For example, this particular code would probably fail almost every test thrown at it. Especially if that is literally all there is of it :)/

Sponsor our Newsletter | Privacy Policy | Terms of Service