Here's my code and I am getting an error undefined variable, this code submits the information submitted by the user to a mysql database.

<?php
include("header.html");
include("nav.html");
include("sidebars.html");
?>
<?php
$errors=array();
if(isset($_POST['submitted'])){
	if(empty($_POST['fname'])){
		$errors[]="You forgot to enter first name";
	}
	   else{
		   $fn=$_POST['fname'];
	   }
	if(empty($_POST['lname'])){
		$errors[]="You forgot to enter last name";
	}
	   else{
		   $ln=$_POST['lname'];
	   }
	if(empty($_POST['email'])){
		$errors[]="You forgot to enter email";
	}
	  else{
		  $email=$_POST['email'];
	  }
if(!empty($_POST['pass1']))
{
	if(($_POST['pass1'])==($_POST['pass2']))
	{
		$pass=$_POST['pass1'];
	}
	   else
	   {
		   $errors[]="The two passwords do not match";
	   }
}
else
{
	$errors[]="You forgot to enter a password";
}
}
if(empty($errors)){
	require_once("connect.php");
	$q="INSERT INTO users VALUES ('$fn','$ln','$email',SHA1('$pass'),NOW())";
	$r=@mysqli_query($dbc,$q);
}
if($r){
	echo "You are now a registered user";
}
else{
	echo "You could not be registered beacuse";
	foreach($errors as $msg){
		echo $msg."<br>";
	}
}
?>
<html>
<form action="register.php" method="post">
<fieldset>
<legend>Form 1</legend>
First Name : <input type="text" name="fname"><br><br>
Last Name  : <input type="text" name="lname"><br><br>
Email      : <input type="text" name="email"><br><br>
Password   : <input type="password" name="pass1"><br><br>
Re-enter pass : <input type="password" name="pass2"><br><br>
<input type="submit" name="submit" value="Register Now">
<input type="hidden" name="submitted" value="TRUE">
</fieldset>
</form>
</html>
<?php
include("footer.html");
?>

the errors i am getting are :

Notice: Undefined variable: fn in C:\xampp\htdocs\register.php on line 45

Notice: Undefined variable: ln in C:\xampp\htdocs\register.php on line 45

Notice: Undefined variable: email in C:\xampp\htdocs\register.php on line 45

Notice: Undefined variable: pass in C:\xampp\htdocs\register.php on line 45
You could not be registered beacuse
Member Avatar for diafol

This will happen when you load the page the first time as the sql will run, even though you haven't sent the form. Place all the code (except form) inside the conditional:

if(isset($_POST['submitted'] && !empty($_POST['submitted']))){
...
//place sql etc here
}

BTW it's not a good idea to place form handling code on the same page as the form. autoresubmit happens on refresh/reload.

Instead of enclosing $fn in '', enclose it in "".
insert into ........ ("$fn","$ln",.......)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.