单击提交按钮成功提交按摩显示,但某些字段为空.还显示未定义的索引和未定义的变量错误



我正在创建一个注册表格。首先显示未定义的索引和未定义的变量错误。我修复了该错误。但是提交数据时发生了另一个错误。当我单击提交按钮时,显示"已成功提交"并向数据库添加新行,但某些字段为空。(用户类型字段(。该数据库字段和错误变量(未定义的索引和未定义的变量错误(是相同的。我该如何解决这个问题..

此代码用于修复未定义的索引和未定义的变量

$usertype = isset( $_POST['usertype'] )? $_POST['usertype']: false;

此代码的错误是什么...在每个php表单中都有此错误。我使用此代码来修复未定义的变量和索引(不使用每个变量,仅使用显示错误的字段(.提交时字段为空。

这是注册.php表格

<?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>

在将一个文件包含在另一个使用相同变量名的文件的情况下,依赖未初始化变量的默认值是有问题的。打开register_globals后,这也是一个重大的安全风险。在使用未初始化的变量时会发出E_NOTICE级错误,但在将元素附加到未初始化的数组时不会发出。isset(( 语言构造可用于检测变量是否已初始化。此外,更理想的是 empty(( 的解决方案,因为如果未初始化变量,它不会生成警告或错误消息。 是的。

现在您必须定义索引变量。 使用变量之前

$usertype="";
这不是
  1. 有效的检查属性checked="Student"
  2. 像这样简单的给出checked="checked"
  3. 设置名称属性以提交输入name="submit"
  4. 由于操作页面与表单页面相同,因此您需要像这样将所有 php 代码包装到帖子检查内部。

注意:您尝试在帖子值可用之前访问它。 您只能在表单提交后访问帖子值

<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
//.... here all of your php code. whatever you want do after form submit.
$connection->close();
}
?>

最新更新