PHP 电子邮件验证 (filter_validate_email) 不起作用


php email validation (filter_validate_email) Not Working

我知道有很多关于这个问题的问题,但我似乎无法选择编码中的错误。我知道这是一件简单的事情,但我看不到它。

我必须创建一个表单,当提交时,数据将被输入到数据库中MySQL但是需要首先验证数据。我对此有 2 个问题,第一个是我的电子邮件验证无法使用:(filter_var($email, filter_validate_email))

问题是,当我提交表单时,无论电子邮件是否有效,它都会返回 true。如果我放(!filter_var($email, filter_validate_email))returns false无论输入如何。

第二个问题是,在加载页面时,它最初会在 SQL 数据库中添加一个空白条目,并添加无效的条目。 即,如果我在提交表单时没有输入名称,则验证将运行并得到error message “name is required”但它仍然在表中创建一个具有空白名称的条目。

我正在使用 PHP 版本 5.3.27

这是我正在做的tafe课程,但是他们目前正在度假,所以任何帮助将不胜感激。

从文件 1 编码:

<body>
<?php
// define variables and set to empty values
$nameErr;
$Name = $Address = $Phone = $Mobile = $Email="example@example.com";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["Name"]))
{$nameErr = "Name is required"; }
else {$Name = test_input($_POST["Name"]);}
if (empty($_POST["Address"]))
 {$Address = "";}
else
 {$Address = test_input($_POST["Address"]);}
if (empty($_POST["Phone"]))
 {$Phone = "";}
else
 {$Phone = test_input($_POST["Phone"]);}
if (empty($_POST["Mobile"]))
 {$Mobile = "";}
else
 {$Mobile = test_input($_POST["Mobile"]);}  

if(filter_var($Email, FILTER_VALIDATE_EMAIL)){ 
 echo"Valid Email";
 }
else{ 
echo "Not a Valid Email";    
}
echo phpinfo();
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form name="addcontact" method="post" action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>", "add-contact.php">
<table border="1" cellpadding="2">
<caption> Add New Caption </caption>
<tr>
<td><label for="Name">Name</label></td>
<td><input type="text" name="Name" size="30" maxlenght="50" tabindex="1"/> <span class="error">*<?php echo $nameErr;?></span>
</td>
</tr>
<tr>
<td><label for="Address">Address</label></td>
<td><textarea name="Address" cols="45" rows="5" tabindex="2"></textarea></td>
</tr>
<tr>
<td><label for="Phone">Phone</label></td>
<td><input type="text" name="Phone" size="20" maxlenght="20" tabindex="3" /> </td>
</tr>
<tr>
<td><label for="Mobile">Mobile</label></td>
<td><input type="text" name="Mobile" size="20" maxlenght="20" tabindex="4" /> </td>
</tr>
<tr>
<td><label for="Email">Email</label></td>
<td><input type="text" name="Email" size="30" maxlenght="50" tabindex="5" /></td>
</tr>
<tr>
<td colspan"2" align="center"><input type="Submit" name="Submit" value="Submit" tabindex="6"/>      
</td>
</tr>

</table>
</form>
<?php 
include("add-contact.php");
?>

</body>
</html>`
And coding from file 2:

<body>
<?php 
$Name = $_POST["Name"];
$Address = $_POST["Address"];
$Phone = $_POST["Phone"];
$Mobile = $_POST["Mobile"];
$Email = $_POST["Email"];

$dbc = mysql_connect("localhost:3306", "root", "webbm01");
if (!$dbc)
die ('Could not connect: ' .mysql_error());

$db_selected = mysql_select_db("tafe", $dbc );
if (!$db_selected)
die ('Could not connect: ' . mysql_error());
$qry
 = "INSERT INTO contacts (Name, Address, Phone, Mobile, Email) VALUES ('" . addslashes($Name) . "', '" . addslashes($Address) . "', '" . addslashes($Phone) . "', '" . addslashes($Mobile). "', '" . addslashes($Email) . "')";
$rst = mysql_query($qry, $dbc);
if ($rst)
{
echo "<b><font color='green'>The contact has been added.</font></b>";
}
else 
{
echo "<b><font color='red'>Error: ". mysql_error($dbc) . ". The contact could not be added.</font></b>";
}
mysql_free_result($rst);

?>
</body>
</html>

检查此代码以进行电子邮件验证等:

<body> <?php
// define variables and set to empty values

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Name"])) {$nameErr = "Name is required"; }else {$Name = htmlspecialchars($_POST["Name"]);}
if (empty($_POST["Address"]))  {$Address = "";}else{$Address = htmlspecialchars($_POST["Address"]);}
if (empty($_POST["Phone"]))  {$Phone = "";}else {$Phone = htmlspecialchars($_POST["Phone"]);}
if (empty($_POST["Mobile"]))  {$Mobile = "";}else {$Mobile = htmlspecialchars($_POST["Mobile"]);}  

if(filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL)){   echo"Valid Email";  }else{ echo "Not a Valid Email"; }
}
?>
<form name="addcontact" method="post" action= "<?php echo $_SERVER["PHP_SELF"];?>">
<table border="1" cellpadding="2"> <caption> Add New Caption </caption> <tr> <td><label for="Name">Name</label></td> <td><input type="text" name="Name" size="30" maxlenght="50" tabindex="1"/> <span class="error">*<?php echo $nameErr;?></span> </td> </tr>
<tr> <td><label for="Address">Address</label></td> <td><textarea name="Address" cols="45" rows="5" tabindex="2"></textarea></td> </tr>
<tr> <td><label for="Phone">Phone</label></td> <td><input type="text" name="Phone" size="20" maxlenght="20" tabindex="3" /> </td> </tr>
<tr> <td><label for="Mobile">Mobile</label></td> <td><input type="text" name="Mobile" size="20" maxlenght="20" tabindex="4" /> </td> </tr> <tr> <td><label for="Email">Email</label></td> <td><input type="text" name="Email" size="30" maxlenght="50" tabindex="5" /></td> </tr> <tr> <td colspan"2" align="center"><input type="Submit" name="Submit" value="Submit" tabindex="6"/>       </td> </tr> </table> </form>
</body> </html>`

验证应在文件中进行:

"添加联系人.php"

因为这是 from 操作在提交时调用的内容。

初始验证器毫无意义,因为 $_POST 数组未初始化。

空 SQL 插入语句的原因是因为您决定执行以下操作:

include("add-contact.php");

在第一个文件中,它在每次加载页面时没有有效的 $_POST 初始化即可运行。

删除该行include("add-contact.php");

这将停止数据库中的空白插入。同时删除操作

<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>

试试action="add-contact.php".电子邮件验证对我来说工作正常。