简体   繁体   English

验证对MYSQL用户输入字段

[英]validate user input fields against MYSQL

I am very new to mysql . 我是mysql的新手。 How to validate a user input email value against the value stored in database. 如何根据存储在数据库中的值来验证用户输入的电子邮件值。 The user enters his email in html5 input field and it is to be validated with the values stored in database and perform success actions accordingly. 用户输入他在HTML5输入域的电子邮件,这是与存储在数据库中的值进行验证,并执行相应的操作成功。 On success navigate to next screen and failure throw a pop up .Attached is screen shot of scenario. 成功时,导航到下一个屏幕,失败时会弹出一个弹出窗口。 [image] ( http://s10.postimg.org/5vvlp200p/user_validation.png ) Please suggest [图像]( http://s10.postimg.org/5vvlp200p/user_validation.png )请提出

EDIT 1: 编辑1:

<!DOCTYPE html>
<html>
<body>
<form action="demo_form.asp" autocomplete="on">
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
</body> 
</html>

EDIT 2: 编辑2:

<?php
try
{
    $connection = mysql_connect("localhost:3306","root","b");
    mysql_select_db("MobileBlog", $connection);


    mysql_query(" // suggest here to validate against emails in db");
    mysql_close($connection);
    echo "SUCCESS";
}
catch(Exception $e)
{
    echo $e->getMessage();
    // Note: Log the error or something
}
?>

EDIT 3 This is my new , HTML5, PHP code , but still failing. 编辑3这是我的新的,HTML5,PHP代码,但还是失败了。

<!DOCTYPE html>
<html>
<body>
<form action="checkform.php" method="post">
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
</body> 
</html>

PHP Code PHP代码

<?php
try
{
$connection = mysql_connect("localhost:3306","root","b");
mysql_select_db("mobileblog", $connection);  
$emailid = $_POST['email']; 
echo $emailid;
$sql = "SELECT Name from table WHERE email=" . $_POST['email'];
$result = mysql_query($sql);
echo $result;
if(!$result) { echo "<p class='error'>Error: No such email address</p>"; }
// note that the if is asking if there is no result
else {
while ($row = mysql_fetch_assoc($result)) { 
echo "<p class='success'>Welcome " . $row['Name'] . "!!</p>";
}
} // end

//mysql_query(" // suggest here to validate against emails in db");
mysql_close($connection);
echo "SUCCESS";
}
catch(Exception $e)
{
echo $e->getMessage();
// Note: Log the error or something
}
?>

ERROR for edit 3 : 错误编辑3:

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in C:\\xampp\\htdocs\\email\\checkform.php on line 5 警告:mysql_connect():在第5行的C:\\ xampp \\ htdocs \\ email \\ checkform.php中,对用户'root'@'localhost'的访问被拒绝(使用密码:是)

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in C:\\xampp\\htdocs\\email\\checkform.php on line 6 abc@gmail.com Error: No such email address 警告:mysql_select_db()期望参数2为资源,布尔值在第6行的C:\\ xampp \\ htdocs \\ email \\ checkform.php中给出abc@gmail.com错误:没有这样的电子邮件地址

Warning: mysql_close() expects parameter 1 to be resource, boolean given in C:\\xampp\\htdocs\\email\\checkform.php on line 32 SUCCESS 警告:mysql_close()期望参数1为资源,在第32行的C:\\ xampp \\ htdocs \\ email \\ checkform.php中给出布尔值

Step 1 第1步

Make your form point towards the page where you are going to process the data. 使您的表单指向要处理数据的页面。 For now we'll keep it simple. 现在,我们将使其保持简单。 The form in your example is OK, you just need to change some of the parts of the <form> tag itself. 在您的示例形式是好的,你只需要改变一些的部位<form>标签本身。

Use the following; 使用以下内容;

<form action="checkform.php" method="post">

Notice the method we used was post . 注意,我们使用的methodpost You'll see that name being used again in a moment. 你会看到,名称为在某一时刻再次使用。

Step 2 第2步

Create a new page with the filename 'checkform.php'. 用文件名“ checkform.php”创建一个新页面。 Assuming you have the correct connection settings, the second bit of code in your question is a good starting point. 假设你有正确的连接设置,代码在你的问题的第二位是一个很好的起点。

Before your mysql_query we need to get the data from the form in order to see if there's a match in the database. 您之前mysql_query我们需要获取为了从表单中的数据,看是否有数据库的匹配。 We do this with the global variable $_POST[] which is an array of all the items on your form with the name attribute as the key. 我们这样做是与全局变量$_POST[]这是与你的窗体上的所有项目的数组name属性的关键。 So the email address as entered by the user will be stored under $_POST['email'] . 因此,作为由用户输入的电子邮件地址将被存储在$_POST['email'] We can now use this as the basis for our database query. 现在,我们可以将其用作数据库查询的基础。

Step 3 第三步

Create a string with the query you are going to send to the database, and use $_POST['email'] where the email address would appear. 使用要发送到数据库的查询创建一个字符串,并使用$_POST['email']出现电子邮件地址的位置。

$sql = "SELECT Name from table WHERE email=" . $_POST['email'];

where table is the name of your table in the database. 其中, table是数据库中的表的名称。

Now you can send that query to the database. 现在,您可以发送查询到数据库。

$result = mysql_query($sql);

Notice that the result of the query will be saved in the $result variable. 请注意,查询结果将保存在$result变量中。 If no match is returned, $result will be false. 如果没有匹配,返回, $result将是错误的。

Step 4 第四步

We now need to get the useful information from the result. 我们现在需要从结果中的有用信息。 We do this by looping over the result and extracting the data. 我们通过遍历结果并提取数据来做到这一点。 First we check if there is a result, if there isn't then we print an error message, if there is we get the data. 首先,我们检查,如果有一个结果,如果没有那么我们打印错误消息,如果我们得到的数据。

We can loop over the result using a while loop and put the data for that row into a $row array with the keys being the names of the columns. 我们可以在使用的结果循环while循环,并把数据为该行到$row阵的钥匙是该列的名称。 We only asked for 'Name' so that will be the only column available to us. 我们仅要求输入“名称”,因此这将是我们唯一可用的列。 The code looks like this; 代码看起来像这样;

if(!$result) { echo "<p class='error'>Error: No such email address</p>"; }
// note that the if is asking if there is no result
else {

    while ($row = mysql_fetch_assoc($result)) { 
        echo "<p class='success'>Welcome " . $row['Name'] . "!!</p>";
    }

} // endif

Please note that if the email address matches more than one row, this code will show more than one welcome message. 请注意,如果电子邮件地址匹配多行,这段代码将显示一个以上的欢迎信息。 I've left it like that for the moment, but you should make the email field unique in your database. 我已经离开它这样的时刻,但你应该让你的数据库的电子邮件领域独树一帜。

Notes 笔记

Please be aware that using mysql_ functions is becoming depreciated and will not be possible in future versions of PHP. 请注意,使用mysql_函数已被弃用,并且在以后的PHP版本中将无法使用。 You will need to use mysqli_ instead. 您将需要使用mysqli_代替。 For the moment you are OK, but once you understand how these database operations work then you should look to move to MySQLi functions. 目前您还可以,但是一旦您了解了这些数据库操作的工作原理,就应该着眼于MySQLi函数。

You can add code in the if part that prompts the user to return to the form if you wish, or a javascript alert (google it) or whatever, and you can style the messages using CSS. 您可以在添加代码if提示用户返回到如果你愿意的形式,或JavaScript警告(谷歌它)或任何部分,你可以使用CSS样式的消息。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM