gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:55:12 25 4
gpt4 key购买 nike

我知道有很多关于此的问题,但是我似乎无法通过我的编码找出错误。我知道这很简单,但我看不到它。

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

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

第二个问题是,加载页面时,它最初会在 SQL 数据库中添加一个空白条目,并且会添加无效的条目。即,如果我在提交表单时没有输入名称,则会运行验证,并且收到错误消息“名称为必填项”,但它仍然会在表中创建一个名称为空的条目。

我使用的是 PHP 版本 5.3.27

这是我正在做的职业类(class),但是他们现在正在假期,所以任何帮助将不胜感激。

从文件 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 电子邮件验证(filter_validate_email)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20631374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com