我正在尝试快速检查用户输入的电子邮件,并在电子邮件字段不唯一时向用户提供错误。我已经设置了数据库以保持这个字段的独特性,即告诉用户他们做错了什么。
如果我在没有电子邮件检查的情况下运行下面的脚本,我可以将值插入表中。所有消息都有效,如果是新电子邮件,则表格会更新。
但是当我添加电子邮件检查代码时,它会识别错误的条目并提供错误,但如果电子邮件是好的话,它将不会继续输入值。
<?php
require('connect.php');
// If the values are posted, insert them into the database.
if (isset($_POST['rname']) && isset($_POST['email'])){
$rname = $_POST['rname'];
$fname = $_POST['fname'];
$email = $_POST['email'];
$emailcheck = "select email from entries where email = ".$email."";
if (mysql_num_rows != 1)
{
$err = "Error email already registered!";
} else {
$query = "INSERT INTO `entries` (rname, fname, email) VALUES ('$rname', '$fname', '$email')";
$result = mysql_query($query);
if($result){
$msg = "Thanks, you have successfully registered.";
}
}
}
?>
此处的故障代码是
$emailcheck = "select email from entries where email = ".$email."";
if (mysql_num_rows != 1)
{
$err = "Error email already registered!";
} else {
答案 0 :(得分:1)
您必须将字符串括在引号内的查询中,并且必须使用mysql_query()执行它。你的代码中都缺少这两个。
$emailcheck = mysql_query("select email from entries where email = '$email'");
您的代码中mysql_num_rows()
的语法也是错误的。它应该是:
int mysql_num_rows ( resource $result )
即
if (mysql_num_rows($emailcheck ) >0 ) // if email already exists
{
$err = "Error email already registered!";
}
答案 1 :(得分:0)
试试这个
请记住,这只是临时表格。也是丑陋的。所以不要担心它使用你的原始形式。
<?php
require('connect.php');
// If the values are posted, insert them into the database.
if (isset($_POST['rname']) && isset($_POST['email'])){
$rname = $_POST['rname'];
$fname = $_POST['fname'];
$email = $_POST['email'];
$emailcheck = mysql_query("select email from entries where email = '$email'");
if (mysql_num_rows($emailcheck) >0)
{
$err = "Error email already registered!";
echo $err;
} else {
$query = "INSERT INTO `entries` (rname, fname, email) VALUES ('$rname', '$fname', '$email')";
$result = mysql_query($query);
if($result){
$msg = "Thanks, you have successfully registered.";
echo $msg;
}
}
}
?>
//the form is here just for reference. as you can see all the type are text. i just made it to check
//weather it going to work or not
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="rname"><br>
<input type="text" name="fname"><br>
<input type="text" name="email"><br>
<input type="submit" value="Upload file">
</form>
答案 2 :(得分:0)
它生成相同的&#34;警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源&#34;错误。谢谢你的尝试。
虽然我仍然不明白为什么我的修复工作在我发布的代码没有,但我找到了解决方案。通过在插入后将重复电子邮件的检查移动到else语句,我能够运行该检查并为用户生成错误消息。由于数据库配置为拒绝重复条目,因此该选项将起作用。我现在可以集中精力逃避所有条目并尝试确保脚本能够抵御黑客攻击。
感谢您的努力。