Simple Input data to database (Error)

Hello,
I am new to this php language although I have been stuck on this particular problem for about 5 days now… I can copy tutorials letter for letter although for some reason I get an error every time no matter who I copy. It relates to Variables being undefined although they are clearly defined in my code below.
Any help would be greatly appreciated.

Current Connection code;

[i][b]<?php

$hostname     = "localhost";
$username     = "root";
$password     = "";
$databasename = "registration";

$conn = mysqli_connect ($hostname, $username, $password, $databasename);[/b][/i]

Current page code;

[b][i]

<?php include("includes/dbh.inc.php"); //variables $firstname = $_POST['Fname']; $lastname = $_POST['Lname']; //Input fields to database $sql = "INSERT INTO `userss`(`Fname`, `Lname`) VALUES ('$firstname','$lastname')"; $result = mysqli_query($conn, $sql); ?> Register
<form action="" method="POST" >
		<input type="text" name="Fname" placeholder="First Name">
		<input type="text" name="Lname" placeholder="Last Name">
		<input type="submit" name="submit"  value="Submit New User">
</form>




	<div class="footer">	
	</div>
</div>
[/i][/b]

Even though I receive this error message & < Undefined index: Lname> I am still able to input information to the data base. Although while the variables are empty this error is displayed. Is there a way around this?

Thank you
Jamie


error.PNG

The problem is that you are trying to use the POST variables before they have been set.

[php]if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’){
// Do insert stuff
}[/php]

Additionally, never ever put variables in a query. You need to use Prepared Statements. I suggest you start using PDO. https://phpdelusions.net/pdo

Do not create variables for nothing.

Does your users table really have two s’s? INSERT INTO users[b]s[/b]

Also, just leave the action out completely (action="")

Sponsor our Newsletter | Privacy Policy | Terms of Service