Hello,
I am using below script to take data from form and input it into database… But i am getting an error in it.
Webpage says,
Undefined index: fname in C:\wamp\www\learn\index.php on line 8
( ! ) Notice: Undefined index: uname in C:\wamp\www\learn\index.php on line 8
( ! ) Notice: Undefined index: email in C:\wamp\www\learn\index.php on line 8
( ! ) Notice: Undefined index: pass1 in C:\wamp\www\learn\index.php on line 8
can some one tell me my error please.
<?php
$connect = mysqli_connect("localhost","root","myusername","mypassword");
if(mysqli_connect_errno())
{echo " No Chance to go"; }
else { echo "Ön the Rock";}
$sql = "INSERT INTO user(fullname,username,email,password}
VALUES('$_POST[fname]','$_POST[uname]','$_POST[email]','$_POST[pass1]')";
?>
So, if you post this form, the username will be available with $_POST[‘username’]; . The attributes “id” aren’t posted to the PHP code, only the “name” attributes.
If you write an index that doesn’t exists, like $_POST[‘whatever’];, you’ll get a similar warning. Like Notice: Undefined index: whatever in
Now, in your HTML form, you use <form> without any method. The default method will be a GET, so you’ll never POST your fields. You should also add a method=“POST” like this:
<form method="POST">
to your form.
The GET method uses the URL and you shouldn’t use it in this case.
You are not executing the query. It would be a good idea to bind those variables as well.
I rarely work with mysqli (I use PDO) but I believe you define variables after binding something like this.
You are still not validating before inserting in the database.
You should NEVER use the above statements without testing that the $_POST value is valid before copying it - otherwise you are just copying potentially invalid or dangerous data.