Undefined variable Error in PHP form

I have created a form in php and html and I am getting following errors.Tried to find solution but in vain. I am working on localhost

Warning: Undefined variable $usersname in C:\xampp\htdocs\signup\signup.php on line 14

Warning: Undefined variable $fname in C:\xampp\htdocs\signup\signup.php on line 14

Warning: Undefined variable $lname in C:\xampp\htdocs\signup\signup.php on line 14

Warning: Undefined variable $email in C:\xampp\htdocs\signup\signup.php on line 14

Warning: Undefined variable $states in C:\xampp\htdocs\signup\signup.php on line 14

Warning: Undefined variable $gender in C:\xampp\htdocs\signup\signup.php on line 14

<?php 

include "config.php";
if(isset($_POST['crateaccount']))
{
$usersname = $_POST['usersname'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['useremail'];
$states = $_POST['states'];
$gender = $_POST['gender'];
}

$insert = "INSERT INTO registrationform (username,firstname,lastname,email,state,gender) VALUES('$usersname','$fname','$lname','$email','$states','$gender') ";

$query = mysqli_query($con, $insert);

if(!$query)
{
echo "Failed";
}

?>

Try using PHP PDO or MYSQLI its more secure. also a better way to parse php variables into SQL statements.

try {
	$conn = new PDO("mysql:host=localhost;dbname=test", "root", "password");
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	$sql = "INSERT INTO users (first_name, last_name, username, state, gender) VALUES ('$firstName', '$lastName', '$username', '$state', '$gender')";

	$conn->exec($sql);
	echo "New record created successfully";

} catch (PDOException $e) {
	echo $sql . "<br>" . $e->getMessage();
}

$conn = null;

also you need to global your config file for that error to stop popping up.

<?php function globals_config($config_file) { global $config; include($config_file); foreach ($config as $key => $value) { global $$key; $$key = $value; } } ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service