- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道有很多关于此的问题,但是我似乎无法通过我的编码找出错误。我知道这很简单,但我看不到它。
我必须创建一个表单,当提交该表单时,数据将被输入到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/
我知道之前已经讨论过这个问题,但自从 2010 年底发表这篇文章以及当时提出问题的其他讨论以来 - Does FILTER_VALIDATE_EMAIL make a string safe for
我试图允许用户在我的 PHP 系统中编辑他们的电子邮件地址,但也阻止他们设置已经存在的电子邮件地址,我还尝试通过 FILTER_VALIDATE_EMAIL 运行它们。然而它对我来说停在某个地方。这些
$str = '"mynam@blabl"@domanin.com'; filter_var($str, FILTER_VALIDATE_EMAIL);//return valid email. 上面
示例有效电子邮件地址: "this is a valid address"@example.com PHP代码:
使用 PHP 的 FILTER_VALIDATE_EMAIL 时允许的最大字符长度是多少? 我正在测试我的脚本以测试允许的最大长度 (200) 的功能,但是当我使用超过 200 个字符的电子邮件时,P
我肯定在这里遗漏了一些东西。由于某种原因 filter_var 不工作。我正在尝试验证来自 $_POST 的电子邮件,即使是有效的电子邮件,它也会返回 false。但是,当我对电子邮件进行硬编码时,它
我使用的是 PHP 5.3.10。这是代码: 它返回:"Email: test@example.c correct. 我认为只有一个字符的顶级域是不正确的(根据此列表,我不知道一个字符长度的 TLD
我知道有很多关于此的问题,但是我似乎无法通过我的编码找出错误。我知道这很简单,但我看不到它。 我必须创建一个表单,当提交该表单时,数据将被输入到MySQL数据库中,但是需要首先验证数据。我有两个问题,
这个问题已经有答案了: How can I validate an email address using a regular expression? (73 个回答) 已关闭 9 年前。 我需要
在 Zend_Validate_EmailAddress 和 filter_var(..., FILTER_VALIDATE_EMAIL) 中,验证电子邮件地址时哪个更好,为什么? 最佳答案 两者都可
我有一个页面,我想在其中接受 GET 参数中的电子邮件地址。如果我使用 FILTER_VALIDATE_EMAIL,我是否仍然容易受到 xss 和 javascript 注入(inject)攻击等攻击
我只是在创建一个注册表单,我只想将有效且安全的电子邮件插入数据库。 一些网站(包括 w3schools)建议在运行 FILTER_VALIDATE_EMAIL 之前运行 FILTER_SANITIZE
我正在使用 filter_var($email, FILTER_VALIDATE_EMAIL) 验证电子邮件。 如果上述函数返回false,我想回应一个声明“无效的电子邮件”。然而,不管页面加载时的
因此,我计划使用 shell_exec() 来处理运行发送电子邮件的 php 脚本。 一切都运行良好,但我只是有点担心仅使用 FILTER_VALIDATE_EMAIL 来确保不会发生注入(injec
因此,我计划使用 shell_exec() 来处理运行发送电子邮件的 php 脚本。 一切都运行良好,但我只是有点担心仅使用 FILTER_VALIDATE_EMAIL 来确保不会发生注入(injec
在阅读了各种帖子后,我决定不使用 REGEX 来检查电子邮件是否有效,而只使用 PHP 的内置 filter_var 函数。它似乎工作正常,直到它开始告诉我一封电子邮件无效,因为我有一个号码。 即 n
我刚刚为一个表单设置验证,我决定尝试使用 filter_var 函数来检查我的电子邮件地址的有效性。我无法找出 filter_var 在任何地方实际允许的内容(因为文档非常简单),而且我发现它允许像
我是一名优秀的程序员,十分优秀!