简体   繁体   English

PHP中的唯一用户ID代码不起作用

[英]Unique user id code in php doesnt work

Recently i was searching for unique username registration using php.. I came across a piece of code which i am displaying below: 最近,我正在使用php搜索唯一的用户名注册。.我遇到了一段代码,显示在下面:

<?php 
$fname=trim($_POST['fname']);
$lname=trim($_POST['lname']);
$email=trim($_POST['email']);
$usn=trim($_POST['usn']);
$dept=trim($_POST['dept']);
$pass=trim($_POST['pass']);
$tel=trim($_POST['tel']);

$dbh = mysql_connect('localhost', 'root','') or die("<h3 style=\"color:red;\" align=\"center\">SERVER ERROR</h3>");
mysql_select_db('fy') or die("<h3 style=\"color:red;\" align=\"center\">SERVER ERROR</h3>");

$error= mysql_query("SELECT * FROM stud WHERE email='$email' OR usn='$usn' OR tel='$tel'") or die (mysql_error()); 

if (mysql_num_rows($error) > 0);
{
    die ("Sorry! Either email, usn or tel already exists!");
}

$query="INSERT INTO stud (fname, lname, email, tel, usn, dept, pass) VALUES ('$fname', '$lname', '$email', '$tel', '$usn', '$dept', '$pass')";
mysql_query($query);
$query="INSERT INTO log VALUES ('$usn','$pass',0,0)";
mysql_query($query);
print("REGISTERED");



 ?>
 <a href="login.php">LOGIN</a><br />

At this moment my database is completely empty. 此时,我的数据库是完全空的。 I've just created the database stud with the desired columns. 我刚刚用所需的列创建了数据库。 Now the problem is when i try to register using my registration page, it gives me the error i specified in die that is 现在的问题是当我尝试使用我的注册页面进行注册时,它给了我我在模具中指定的错误,即

"Sorry! Either email, usn or tel already exists!"

How is this possible if there are no values in the database. 如果数据库中没有值,怎么办? In the registration form I've given 在我提供的注册表中

action="register.php" 

as a processing file. 作为处理文件。 Also I've tried with mysql_fetch_assoc() , but i get the same error. 我也尝试了mysql_fetch_assoc() ,但是我得到了同样的错误。 Any help is appreciated. 任何帮助表示赞赏。 Thank you . 谢谢 。

Your first problem is that, as John Conde states, your code is vulnerable to SQL injection attacks. 您的第一个问题是,正如John Conde所说,您的代码容易受到SQL注入攻击的攻击。

Your second problem, and to answer your question, is probably because you have this: 您要回答的第二个问题可能是因为您有以下问题:

if (mysql_num_rows($error) > 0);

instead of this: 代替这个:

if (mysql_num_rows($error) > 0)

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

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