I was create a form for the registration.Firstly shows undefined index and undefined variable error.I fixed that error.But another error was occurred when submitting data.. When I clicked the submit button shows "submitted successfully" and add new row to the database but some fields are empty.(user type field).That database field and error variable (undefined index and undefined variable error) are same..How can I fix this problem..
This code was used to fix both undefined index and undefined variable
$usertype = isset( $_POST['usertype'] )? $_POST['usertype']: false;
what is the error with this code...In every php form has this error.I used this code for fix undefined variable and index(Not use every variables.only use that shows the errors).that fields are empty when the submitting.
This is Registration.php form
<?php
$username=$_POST['username'];
$password=$_POST['password'];
$usertype = isset( $_POST['usertype'] )? $_POST['usertype']: false;
$connection = new mysqli("localhost", "root", "","student_information");
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$sql= "INSERT INTO registration(User_Name,Password,User_Type)
VALUES ('$username','$password','$usertype')";
if ($connection->multi_query($sql) === TRUE) {
echo "Submitted Successfully";
} else {
echo "Error: " .$sql."<br>". $connection->error;
}
$connection->close();
?>
<!DOCTYPE html>
<html >
<head>
<meta charset = "utf-8">
<title>Registration Form </title>
<link rel="stylesheet" href = "Registration.css" >
</head>
<body>
<div class = "RegistrationBox" >
<h2> Registration </h2>
<form action="Registration.php" method="post">
<p>User Name </p>
<input type = "text" name = "username" id="username" placeholder = "Enter User Name">
<p>Password </p>
<input type = "password" name = "password" id="password" placeholder = "********************">
<p>Confirm Password </p>
<input type = "password" name = "confirmpassword" id="confirmpassword" placeholder = "********************">
<p>User Type </p><br>
<input type="radio" name="usertype " id="usertype" value="Student" checked="Student" /> Student
<input type="radio" name="usertype " id="usertype" value="Lecturer" /> Lecturer<br>
<input type = "submit" value = "Submit" id="submit">
<br>
<a href = "#" >More </a>
</form>
</div>
</body>
</html>
</div>
Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized. Additionally and more ideal is the solution of empty() since it does not generate a warning or error message if the variable is not initialized. yes.
now you have to define index variable. before use of variable
$usertype="";
checked="Student"
checked="checked"
name="submit"
Note : Your trying to access the post value on before it available. you only access the post value after form submit
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
//.... here all of your php code. whatever you want do after form submit.
$connection->close();
}
?>