我正在尝试进行表单验证并使用php将验证后的数据存储在mysql数据库中。代码工作正常,因为它应该在验证过程后将表单数据保存在mysql数据库中。问题是它显示未定义这些行中的索引错误
1。<span class="error">* <?php echo $error['name'];?></span>
2。<span class="error">* <?php echo $error['email']; ?></span>
3。<span class="error"><?php echo $error['website']; ?></span>
4。<span class="error">* <?php echo $error['gender'];?></span>
。
这是我的完整代码。
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$error=array();
$name = $email = $gender = $comment = $website = $data = "";
function test_input($data)
{
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$error['name']= "Name is required";}
else
{
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error['name'] = "Only letters and white space allowed";
}
}
if (empty($_POST["email"]))
{$error['email'] = "Email is required";}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$error['email'] = "Invalid email format";
}
}
if (empty($_POST["website"]))
{$website = "";}
else
{
$website = test_input($_POST["website"]);
// check if URL address syntax is valid
(this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)
[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$error['website'] = "Invalid URL";
}
}
if (empty($_POST["comment"]))
{$comment = "";}
else
{$comment = test_input($_POST["comment"]);}
if (empty($_POST["gender"]))
{$error['gender'] = "Gender is required";}
else
{$gender = test_input($_POST["gender"]);}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $error['name'];?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $error['email']; ?></span>
<br><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $error['website']; ?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="female")
echo "checked";?> value="female">Female
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $error['gender'];?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
$con=mysqli_connect("localhost","root","root","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO np_appoint (Name,Email,Website,Comment,Gender)
VALUES
('$_POST[Name]','$_POST[Email]','$_POST[website]','$_POST[Comments]','$_POST[Gender]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
</body>
</html>
答案 0 :(得分:0)
要避免未定义的值,请使用isset().
<span class="error"> <?php if(isset($error['name']))
echo $error['name'];?></span>
答案 1 :(得分:0)
因为如果没有错误,则$error
数组未定义。
尝试在代码的开头定义代码&gt;
$error['name'] = '';
$error['email'] = '';
$error['website'] = '';
$error['gender'] = '';
答案 2 :(得分:0)
您的错误消息一次设置一个,但$error
数组中的键未预定义。因此,如果名称有效,您将不会拥有$error['name']
。这将产生一个未定义的索引NOTICE。
答案 3 :(得分:0)
尝试访问尚未定义的数组键与访问任何其他未定义的变量相同,会产生E_NOTICE级错误消息,如undefined index
试试这个count($error)>0
<?php if(count($error)>0){?>
<span class="error">* <?php echo $error['name'];?></span>
<?php } ?>
OR
您可以使用foreach
<?php foreach($error as $value){?>
<span class="error">* <?php echo $value;?></span>
<?php } ?>
答案 4 :(得分:0)
USe isset()函数
<span class="error">* <?php if(isset($error['name']))
echo $error['name'];?></span>
或使用@
(不推荐)
<span class="error">* <?php echo @$error['name'];?></span>